dongyu4863 2017-09-18 03:39
浏览 321

Websocket消息发送到特定房间-Golang Kataras / Iris

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!
", 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:

  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条回答 默认 最新

  • donpvtzuux37724 2018-05-02 15:15
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用