https://github.com/damoye/gotalk
Simple Go network communication library.
https://github.com/damoye/gotalk
go network
Last synced: 12 months ago
JSON representation
Simple Go network communication library.
- Host: GitHub
- URL: https://github.com/damoye/gotalk
- Owner: damoye
- License: mit
- Created: 2017-03-17T09:05:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-21T07:32:29.000Z (over 9 years ago)
- Last Synced: 2024-06-20T13:39:56.353Z (about 2 years ago)
- Topics: go, network
- Language: Go
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gotalk
[](https://godoc.org/github.com/damoye/gotalk)
[](https://travis-ci.org/damoye/gotalk)
[](https://coveralls.io/github/damoye/gotalk)
Simple Go network communication library.
Gotalk makes up for the missing message boundaries of TCP. It can be combined with serialization tools like JSON and Protocol Buffers. It makes network communication much easier. Its inspiration comes from the [Bulk Strings from RESP](https://redis.io/topics/protocol#resp-bulk-strings)
## Protocol
Gotalk defines a protocol like this:
```go
(length_of_message)\r\n(message)\r\n
```
For example:
message | bytes
--------------|------------------------
"hello world" | "11\r\nhello world\r\n"
"" | "0\r\n\r\n"
## Installation
```sh
go get github.com/damoye/gotalk
```
## Example
```go
package main
import (
"bufio"
"fmt"
"log"
"net"
"github.com/damoye/gotalk"
)
func handleConnection(conn net.Conn) {
reader := bufio.NewReader(conn)
defer conn.Close()
for {
message, err := gotalk.Decode(reader)
if err != nil {
log.Print(err)
break
}
_, err = conn.Write(gotalk.Encode(message))
if err != nil {
log.Print(err)
break
}
}
}
func main() {
l, err := net.Listen("tcp", fmt.Sprint(":", 2000))
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go handleConnection(conn)
}
}
```