Skip to content

xJayLee/link

 
 

Repository files navigation

THIS PROJECT IS STILL IN EARLY STAGE

API MAY RADICALLY CHANGE IN THE NEAR FUTURE

Introduction

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.

How to install

go get github.com/funny/link

How to use

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!" })

Examples

Document

Let's Go!

About

A golang network library for packet based persistent connection communication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published