Go 網絡庫:go link

jopen 10年前發布 | 19K 次閱讀 go link Google Go/Golang開發

link 是簡單的 GO 網絡庫,專注于基于數據包的持久連接通訊。link 提供一個數據包分割協議,比如 Erlang 默認的 {packet: N} ,同時還支持自定義數據包分割協議。但是 link 不會限制編碼或者解碼格式的請求和應答。這個庫還提供會話管理和播放功能。

Choose a protocol for your project.

proto := link.PacketN(2, binary.BigEndian) 

Setup a server on port 8080 and set protocol.

server, _ := link.Listen("tcp", "0.0.0.0:8080", proto) 

Handle incoming connections. And setup a message handler on the new session.

server.AcceptLoop(func(session *Session) {
    fmt.Println("session start")

    session.ReadLoop(func(session *Session, msg []byte) {
        fmt.Printf("new message: %s\n", msg)
    })

    fmt.Println("session closed")
})

Use the same protocol dial to the server.

 proto := link.PacketN(2, binary.BigEndian)

client, _ := link.Dial("tcp", "127.0.0.1:8080", proto)

Send a message to server.

client.Send(link.Binary("Hello World!")) 

項目主頁:http://www.baiduhome.net/lib/view/home/1411390194125

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!