如何解决Gorilla/websocket 中的回调
我正在尝试在 Go 中构建一个多人游戏后端。我以前在 Node JS (socket.io) 中工作过,我记得能够在我的前端为不同的消息注册回调,如下所示:
componentDidMount() {
this.socket.on('join_room',(clients) => {
//do smt
});
this.socket.on('user_image',(data) => {
// do smt
});
}
在 gorilla/websockets 中,我只能从后端接收一般消息。如何区分消息类型?有没有办法注册某种类似于socket.io的回调?
let conn = new WebSocket("ws://" + document.location.host + "/ws/" + roomId);
conn.onclose = function (evt) {
let item = document.createElement("div");
item.innerHTML = "<b>Connection closed.</b>";
appendLog(item);
};
conn.onmessage = function (evt) {
let messages = evt.data.split('\n');
for (let i = 0; i < messages.length; i++) {
let item = document.createElement("div");
item.innerText = messages[i];
appendLog(item);
}
我使用这个 repo 作为我的后端代码示例:https://github.com/AnupKumarPanwar/Golang-realtime-chat
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。