猿问

Golang websocket 处理程序

在使用 Node.js 编写了很长时间后,我开始学习 Golang,我对如何实现处理程序有点好奇——我选择使用 Gorilla Websocket,因为我知道它是最可靠的包那里。

例如,在 socket.io 中,您有一个简单的 socket.on 函数,它允许我根据 JSON 中传递的“名称”参数调用函数。

Gorilla websocket 没有实现这样的东西,所以我的问题是我是否要实现 socket.io 背后的逻辑以实现我想要的?

就像根据 websocket 中传输的值执行某个程序一样?

如果是这样 - 我需要在客户端(我在前端使用 AngularJS)和服务器端分别实现它 - 根据我在 AngularJS 中在 JSON 中获得的值创建一个 switch case 语句作为前端 - end 和 in Go 作为后端,还有 - 这是最有效的方式吗?


一只名叫tom的猫
浏览 204回答 2
2回答

翻翻过去那场雪

如果您已经使用 Javascript 一段时间了,那么实现您自己的 Javascript 版本真的很容易socket.on,socket.emit这里是我为自己的项目制作的版本,但如果您需要,您可以拥有它,// e.g.// let socket = new Socket("ws://w/e");// socket.on('connected', () => { console.log('Connected'); });// socket.emit('lobby join', { data: { username: 'Boo' } });// Using ES2015 with Babelimport {EventEmitter} from 'events';class Socket {&nbsp; &nbsp; constructor(wsurl, ee = new EventEmitter()) {&nbsp; &nbsp; &nbsp; &nbsp; let ws = new WebSocket(wsurl);&nbsp; &nbsp; &nbsp; &nbsp; this.ee = ee;&nbsp; &nbsp; &nbsp; &nbsp; this.ws = ws;&nbsp; &nbsp; &nbsp; &nbsp; ws.onmessage = this.message.bind(this);&nbsp; &nbsp; &nbsp; &nbsp; ws.onopen = this.open.bind(this);&nbsp; &nbsp; &nbsp; &nbsp; ws.onclose = this.close.bind(this);&nbsp; &nbsp; }&nbsp; &nbsp; on(name, fn) {&nbsp; &nbsp; &nbsp; &nbsp; this.ee.on(name, fn);&nbsp; &nbsp; }&nbsp; &nbsp; off(name, fn) {&nbsp; &nbsp; &nbsp; &nbsp; this.ee.removeListener(name, fn);&nbsp; &nbsp; }&nbsp; &nbsp; emit(name, data) {&nbsp; &nbsp; &nbsp; &nbsp; const message = JSON.stringify({name, data});&nbsp; &nbsp; &nbsp; &nbsp; this.ws.send(message);&nbsp; &nbsp; }&nbsp; &nbsp; message(e) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const msgData = JSON.parse(e.data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.ee.emit(msgData.name, msgData.data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let error = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.ee.emit(error.message)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; open() {&nbsp; &nbsp; &nbsp; &nbsp; this.ee.emit('connected');&nbsp; &nbsp; }&nbsp; &nbsp; close() {&nbsp; &nbsp; &nbsp; &nbsp; this.ee.emit('disconnected');&nbsp; &nbsp; }&nbsp; &nbsp;}export default Socket这将让你使用你的共同socket.on('event', fn);和什么不至于在服务器端处理:对于接收消息,我个人只是创建一个 switch 语句,将传入的字符串与函数匹配,例如:// readPump pumps messages from the websocket connection to the hub.func (c *connection) readPump() {&nbsp; &nbsp; defer c.ws.Close()&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; _, message, err := c.ws.ReadMessage()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var incMessage interface{}&nbsp; &nbsp; &nbsp; &nbsp; err = json.Unmarshal(message, &incMessage)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; incMessageMap := incMessage.(map[string]interface{})&nbsp; &nbsp; &nbsp; &nbsp; switch incMessageMap["name"] {&nbsp; &nbsp; &nbsp; &nbsp; case "lobby join":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Do something to handle joining&nbsp; &nbsp; &nbsp; &nbsp; case "lobby leave":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Do something to handle leaving&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}为了发送它们send channel,我的连接上有一个存储在地图中的连接,当我需要发出时,我有一个简单的结构,它采用消息名称和数据,例如:type wsMsg struct {&nbsp; &nbsp; Name string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`json:"name"`&nbsp; &nbsp; Data map[string]interface{} `json:"data"`}c.send <- wsMsg{&nbsp; &nbsp; &nbsp;"user joined",&nbsp; &nbsp; &nbsp;map[string]interface{}{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "username": "Booh",&nbsp; &nbsp; &nbsp;},}然后在客户端它会变成socket.on('user joined', (msg) => {&nbsp; &nbsp; console.log(msg) // { username: "Booh" }});我建议查看 gorillas git 上的示例:https : //github.com/gorilla/websocket/tree/master/examples/chat

30秒到达战场

这是一个 golang websocket 流视频的工作示例&nbsp;https://github.com/interviewparrot/OpenAVStream让我知道它是否足够好
随时随地看视频慕课网APP

相关分类

Go
我要回答