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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)