将消息发送到指定房间的Websocket – Golang Kataras/Iris

huangapple go评论139阅读模式
英文:

Websocket message to Specific Room - Golang Kataras/Iris

问题

我正在尝试向特定的聊天室发送消息,但是它不起作用,并且它会将消息发送到所有的聊天室,同时我的消息会被我的名字和第一个聊天室用户的名字接收两次。来自一个聊天室的消息会被广播到所有的聊天室。

我使用了这里的示例代码- https://github.com/kataras/iris/blob/master/_examples/websocket/secure/main.gohttps://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>

错误:

  1. 它将消息发送到所有的聊天室,而不是特定的聊天室。

  2. 第一个创建聊天室的用户会自动加入所有的聊天室。

  3. 在其他聊天室发送消息的人会看到他们的消息被“FirstUser”重复/克隆在他们的聊天室中。(无论他是否是聊天组的成员)

期望:

  1. 人们只能向他们加入的聊天室发送/接收消息。

  2. 第一个用户不应该自动加入聊天室。

  3. 人们不应该看到他们的消息再次被重复,带有“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 + &quot; has Joined Chat!&quot;))
			c.OnMessage(func(data []byte) {
				message := string(data)
				if message == &quot;leave&quot; {
					c.Leave(myChatRoom)
					c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + &quot; has Left Chat!&quot;))
					return
				}
				c.To(myChatRoom).EmitMessage([]byte(user.(users).Username + &quot;: &quot; + message))
			})
			c.OnDisconnect(func() {
				fmt.Printf(&quot;Connection with ID: %s has been disconnected!\n&quot;, c.ID())
			})
		})

HTML Code:

&lt;div id=&quot;messages&quot; style=&quot;border-width: 1px; border-style: solid; height: 200px;overflow:auto&quot;&gt;&lt;/div&gt;
    &lt;input type=&quot;text&quot; id=&quot;messageTxt&quot; /&gt;
    &lt;button type=&quot;button&quot; id=&quot;sendBtn&quot;&gt;Send&lt;/button&gt;

Javascript Code:

&lt;script&gt;
  var messageTxt;
  var messages;
  var HOST = &#39;localhost&#39;
  jQuery(function() {
    messageTxt = jQuery(&quot;#messageTxt&quot;);
    messages = jQuery(&quot;#messages&quot;);
    w = new WebSocket(&quot;ws://&quot; + HOST + &quot;/my_endpoint&quot;);
    w.onopen = function() {
      console.log(&quot;Websocket connection enstablished&quot;);
    };
    w.onclose = function() {
      appendMessage(jQuery(&quot;&lt;div&gt;&lt;center&gt;&lt;h3&gt;Disconnected&lt;/h3&gt;&lt;/center&gt;&lt;/div&gt;&quot;));
    };
    w.onmessage = function(message) {
      console.log(&quot;Message Appended: &quot; + message)
      appendMessage(jQuery(&quot;&lt;div&gt;&quot; + message.data + &quot;&lt;/div&gt;&quot;));
    };
    jQuery(&quot;#sendBtn&quot;).click(function() {
      w.send(messageTxt.val().toString());
      messageTxt.val(&quot;&quot;);
    });
  })

  function appendMessage(messageDiv) {
    messageDiv.appendTo(jQuery(&quot;#messages&quot;));
  }
&lt;/script&gt;

Error:

  1. It sends message to all ROOM and not specific Room.

  2. User who created room first automatically joins all the ROOM

  3. 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:

  1. People can send/receive message to only those room where they have joined.

  2. First User should not be able to join CHATRoom automatically.

  3. 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.

huangapple
  • 本文由 发表于 2017年9月18日 11:39:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/46271256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定