英文:
Websocket message to Specific Room - Golang Kataras/Iris
问题
我正在尝试向特定的聊天室发送消息,但是它不起作用,并且它会将消息发送到所有的聊天室,同时我的消息会被我的名字和第一个聊天室用户的名字接收两次。来自一个聊天室的消息会被广播到所有的聊天室。
我使用了这里的示例代码- https://github.com/kataras/iris/blob/master/_examples/websocket/secure/main.go 和 https://github.com/kataras/iris/blob/master/_examples/websocket/native-messages/main.go
下面是我正在使用的代码,它给我报错:
var myChatRoom = strconv.Itoa(room.ID)
ws := websocket.New(websocket.Config{})
ws.OnConnection(func(c websocket.Connection) {
c.Join(myChatRoom)
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + " has Joined Chat!"))
c.OnMessage(func(data []byte) {
message := string(data)
if message == "leave" {
c.Leave(myChatRoom)
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + " has Left Chat!"))
return
}
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + ": " + message))
})
c.OnDisconnect(func() {
fmt.Printf("Connection with ID: %s has been disconnected!\n", c.ID())
})
})
HTML代码:
<div id="messages" style="border-width: 1px; border-style: solid; height: 200px;overflow:auto"></div>
<input type="text" id="messageTxt" />
<button type="button" id="sendBtn">Send</button>
JavaScript代码:
<script>
var messageTxt;
var messages;
var HOST = 'localhost'
jQuery(function() {
messageTxt = jQuery("#messageTxt");
messages = jQuery("#messages");
w = new WebSocket("ws://" + HOST + "/my_endpoint");
w.onopen = function() {
console.log("Websocket connection enstablished");
};
w.onclose = function() {
appendMessage(jQuery("<div><center><h3>Disconnected</h3></center></div>"));
};
w.onmessage = function(message) {
console.log("Message Appended: " + message)
appendMessage(jQuery("<div>" + message.data + "</div>"));
};
jQuery("#sendBtn").click(function() {
w.send(messageTxt.val().toString());
messageTxt.val("");
});
})
function appendMessage(messageDiv) {
messageDiv.appendTo(jQuery("#messages"));
}
</script>
错误:
-
它将消息发送到所有的聊天室,而不是特定的聊天室。
-
第一个创建聊天室的用户会自动加入所有的聊天室。
-
在其他聊天室发送消息的人会看到他们的消息被“FirstUser”重复/克隆在他们的聊天室中。(无论他是否是聊天组的成员)
期望:
-
人们只能向他们加入的聊天室发送/接收消息。
-
第一个用户不应该自动加入聊天室。
-
人们不应该看到他们的消息再次被重复,带有“FirstUser”的名字。
英文:
I am trying to send Message to Particular Room, but it doesn't work and It sends message to all room along with my message being received twice from my name and with first chat room user name. Message from 1 Room is broadcasted to all Chat Room.
I have used Example Code from here- https://github.com/kataras/iris/blob/master/_examples/websocket/secure/main.go. and
https://github.com/kataras/iris/blob/master/_examples/websocket/native-messages/main.go
Below is the Code, I am using, that is giving me the error :
var myChatRoom = strconv.Itoa(room.ID)
ws := websocket.New(websocket.Config{})
ws.OnConnection(func(c websocket.Connection) {
c.Join(myChatRoom)
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + " has Joined Chat!"))
c.OnMessage(func(data []byte) {
message := string(data)
if message == "leave" {
c.Leave(myChatRoom)
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + " has Left Chat!"))
return
}
c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + ": " + message))
})
c.OnDisconnect(func() {
fmt.Printf("Connection with ID: %s has been disconnected!\n", c.ID())
})
})
HTML Code:
<div id="messages" style="border-width: 1px; border-style: solid; height: 200px;overflow:auto"></div>
<input type="text" id="messageTxt" />
<button type="button" id="sendBtn">Send</button>
Javascript Code:
<script>
var messageTxt;
var messages;
var HOST = 'localhost'
jQuery(function() {
messageTxt = jQuery("#messageTxt");
messages = jQuery("#messages");
w = new WebSocket("ws://" + HOST + "/my_endpoint");
w.onopen = function() {
console.log("Websocket connection enstablished");
};
w.onclose = function() {
appendMessage(jQuery("<div><center><h3>Disconnected</h3></center></div>"));
};
w.onmessage = function(message) {
console.log("Message Appended: " + message)
appendMessage(jQuery("<div>" + message.data + "</div>"));
};
jQuery("#sendBtn").click(function() {
w.send(messageTxt.val().toString());
messageTxt.val("");
});
})
function appendMessage(messageDiv) {
messageDiv.appendTo(jQuery("#messages"));
}
</script>
Error:
-
It sends message to all ROOM and not specific Room.
-
User who created room first automatically joins all the ROOM
-
People sending message in other ROOM see their message being Repeated/cloned in their ROOM by "FirstUser" who created first room in chat. (Irrespective of whether he is member of the chat group or not)
Expecting:
-
People can send/receive message to only those room where they have joined.
-
First User should not be able to join CHATRoom automatically.
-
People should not see their message being repeated again with "FirstUser" name.
答案1
得分: 1
这是一个微小的错误,刚刚修复。请使用以下命令进行升级:
go get -u github.com/kataras/iris
还发布了一个新版本"v10.6.3"。
非常感谢 @Belarus,你太棒了!
诚挚地,
Gerasimos Maropoulos,
Iris web框架的作者
英文:
It was a tiny bug, fixed just moments ago. Please upgrade with:
go get -u github.com/kataras/iris
A new release "v10.6.3" was also pushed.
Thank you a lot @Belarus, you're great!
Sincerely,
Gerasimos Maropoulos,
Author of the Iris web framework.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论