socket.io在golang中的一个实现,一个实时应用程序框架(go-socket.io)

网友投稿 2025 2022-10-10

socket.io在golang中的一个实现,一个实时应用程序框架(go-socket.io)

socket.io在golang中的一个实现,一个实时应用程序框架(go-socket.io)

go-socket.io

go-socket.io is an implementation of Socket.IO in Golang, which is a realtime application framework.

Currently this library supports 1.4 version of the Socket.IO client. It supports room and namespaces.

Go 1.9+ is required!

Help wanted This project is looking for contributors to help fix bugs and implement new features. Please check Issue 192. All help is much appreciated.

for compatibility with Socket.IO 0.9.x, please use branch 0.9.x *

Contents

InstallLast changesExampleContributorsLicense

Install

Install the package with:

go get github.com/googollee/go-socket.io

Import it with:

import "github.com/googollee/go-socket.io"

and use socketio as the package name inside the code.

Last changes

Important changes:

Short infoDescriptionDate
Changed signature of OnErrorChanged signature of OnError From: server.OnError(string, func(error)) To: server.OnError(string, func(Conn, error))2019-10-16

Example

Please check the example folder for details.

package mainimport ( "fmt" "log" "net/http" "github.com/googollee/go-socket.io")func main() { server, err := socketio.NewServer(nil) if err != nil { log.Fatal(err) } server.OnConnect("/", func(s socketio.Conn) error { s.SetContext("") fmt.Println("connected:", s.ID()) return nil }) server.OnEvent("/", "notice", func(s socketio.Conn, msg string) { fmt.Println("notice:", msg) s.Emit("reply", "have "+msg) }) server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string { s.SetContext(msg) return "recv " + msg }) server.OnEvent("/", "bye", func(s socketio.Conn) string { last := s.Context().(string) s.Emit("bye", last) s.Close() return last }) server.OnError("/", func(s socketio.Conn, e error) { fmt.Println("meet error:", e) }) server.OnDisconnect("/", func(s socketio.Conn, reason string) { fmt.Println("closed", reason) }) go server.Serve() defer server.Close() http.Handle("/socket.io/", server) http.Handle("/", http.FileServer(http.Dir("./asset"))) log.Println("Serving at localhost:8000...") log.Fatal(http.ListenAndServe(":8000", nil))}

Acknowledgements in go-socket.io 1.X.X

See documentation about acknowledgements

Sending ACK with data from SERVER to CLIENT

Client-side

//using client-side socket.io-1.X.X.js socket.emit('some:event', JSON.stringify(someData), function(data){ console.log('ACK from server wtih data: ', data)); });

Server-side

// The return type may vary depending on whether you will return// In golang implementation of Socket.IO don't used callbacks for acknowledgement,// but used return value, which wrapped into ack package and returned to the client's callback in JavaScriptso.On("some:event", func(msg string) string { return msg //Sending ack with data in msg back to client, using "return statement"})

Sending ACK with data from CLIENT to SERVER

Client-side

//using client-side socket.io-1.X.X.js//last parameter of "on" handler is callback for sending ack to server with data or without datasocket.on('some:event', function (msg, sendAckCb) { //Sending ACK with data to server after receiving some:event from server sendAckCb(JSON.stringify(data)); // for example used serializing to JSON}

Server-side

//You can use Emit or BroadcastTo with last parameter as callback for handling ack from client//Sending packet to room "room_name" and event "some:event"so.BroadcastTo("room_name", "some:event", dataForClient, func (so socketio.Socket, data string) { log.Println("Client ACK with data: ", data)})// Orso.Emit("some:event", dataForClient, func (so socketio.Socket, data string) { log.Println("Client ACK with data: ", data)})

Broadcast to All connected Client

Server-side

//Add all connected user to a room, in example? "bcast"server.OnConnect("/", func(s socketio.Conn) error { s.SetContext("") fmt.Println("connected:", s.ID()) s.Join("bcast") return nil})//Broadcast message to all connected userserver.BroadcastToRoom("", "bcast", "event:name", msg)

Client-side

socket.on('some:event', function (msg) { console.log(msg);});

Cautch Disconnected reason

Server-side

so.OnDisconnect("/", func(so socketio.Conn, reason string) { log.Println("closed", reason)})

Possible reasons:

ReasonSideDescription
client namespace disconnectClient SideGot disconnect packet from client

Community

Telegram chat: @go_socketio

Contributors

This project exists thanks to all the people who contribute. [Contribute].

License

The 3-clause BSD License - see LICENSE for more details

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【算法入门11】反转链表
下一篇:多项式加法运算(链表实现)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~