https://github.com/aravinth2094/nio
Golang NIO Client
https://github.com/aravinth2094/nio
asynchronous client client-server golang nio socket
Last synced: over 1 year ago
JSON representation
Golang NIO Client
- Host: GitHub
- URL: https://github.com/aravinth2094/nio
- Owner: aravinth2094
- License: mit
- Created: 2021-06-04T17:26:29.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-04T18:03:27.000Z (about 5 years ago)
- Last Synced: 2025-01-28T10:44:38.206Z (over 1 year ago)
- Topics: asynchronous, client, client-server, golang, nio, socket
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang NIO Client
* This is a Non-blocking IO socket client library that uses function callbacks to achieve asynchronous behaviour (javascript much?)
* You can chain functions that you would like your data to pass through during read/write phase.
```golang
package main
import (
"crypto/tls"
"io"
"log"
"sync"
nio "github.com/aravinth2094/nio"
)
func main() {
printString := func(data []byte, err error) ([]byte, bool, error) {
if err != nil {
if err == io.EOF {
log.Fatalln(err)
}
return nil, false, err
}
log.Println(string(data))
return data, false, nil
}
printBytesWritten := func(bytesWritten int, err error) {
if err != nil {
if err == io.EOF {
log.Fatalln(err)
} else {
log.Println(err)
}
} else {
log.Println(bytesWritten, "bytes written")
}
}
wg := &sync.WaitGroup{}
wg.Add(1)
channel, err := nio.GetChannel("tcp", "localhost", 8080, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
log.Fatalln(err)
}
write := channel.GetWriter(nio.ConvertToUpperCase, printString)
channel.Read(wg, nio.ConvertToUpperCase, printString, func(data []byte, err error) ([]byte, bool, error) {
if err != nil {
return data, false, err
}
write(append([]byte("hello "), data...), printBytesWritten)
return data, false, nil
})
channel.Write([]byte("Hi"), printBytesWritten)
wg.Wait()
}
```