THIS PROJECT IS STILL IN EARLY STAGE
API MAY RADICALLY CHANGE IN THE NEAR FUTURE
This is a network library for Go.
It focus on packet based persistent connection communication.
It provide a packet splitting protocol like Erlang's {packet: N}
. And supported custom packet splitting protocol.
But it didn't limit the encode or decode format of the request and response.
Also this library provide session management and broadcast features to make your life easy.
go get github.com/funny/link
Choose a protocol for your project.
proto := link.NewFixProtocol(4, binary.BigEndian)
Setup a server on port 8080
and set protocol.
server, _ := link.ListenAndServe("tcp", "0.0.0.0:8080", proto)
server.Start()
Handle incoming connections. And setup a message handler on the new session.
server.Handle(func(session *Session) {
fmt.Println("new session in")
session.OnMessage(func(session *Session, msg []byte) {
fmt.Printf("new message: %s\n", msg)
})
})
Use the same protocol dial to the server.
proto := link.NewFixProtocol(4, binary.BigEndian)
client, _ := link.Dial("tcp", "127.0.0.1:8080", proto)
client.Start()
Implement a message type.
type TestMessage struct {
Message string
}
func (msg TestMessage) RecommendPacketSize() uint {
return uint(len(msg.Message))
}
func (msg TestMessage) AppendToPacket(packet []byte) []byte {
return append(packet, msg.Message...)
}
Send a message to server.
client.Send(TestMessage{ "Hello World!" })