Websocket在playframework 2中发送所有客户端的数据 [英] Websocket send data all client in playframework 2

查看:139
本文介绍了Websocket在playframework 2中发送所有客户端的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来理解playframework中的WS

i need help to understand WS in playframework

我的控制器中有下一个代码

i have the next code in my controller

public static WebSocket<String> sockHandler() {
return new WebSocket<String>() {
    // Se llama para establecer el WS

    public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {

        //por cada evento recivido por el socket
        // Se regitra una llamada para el procesamiento de los eventos
        in.onMessage(new Callback<String>() {
            public void invoke(String event) {
                //Logger.info(event)
                System.out.println("este es el event "+event);

            } 
         });

        // write out a greeting
        out.write("Hola a todos");
    }
};

}

在我看来,我拥有:

<script type="text/javascript" charset="utf-8">

    $(function() {
        var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket
        var sock = new WS("@routes.Application.sockHandler().webSocketURL(request)")

        $('button.send').click(function() {
          console.log('entro al click');
          sendMessage();  
        }); 

        var sendMessage = function() {
            sock.send("llamando controller");
        }

        var receiveEvent= function(event) {
            $('.greeting').append(event.data);
            alert('entro');
        }

        sock.onmessage=receiveEvent;
    })

</script>

当客户端发送动作时,我需要始终在所有客户端上打印警报('entro') 。

I need to always print the alert('entro') on all clients when a client sends an action.

请原谅我的英语,但我会讲西班牙语。

Excuse my English, but I speak Spanish.

非常感谢

推荐答案

在您的代码中,您正在编写对实际创建的Websocket(即调用客户端和服务器之间的实际Websocket)的响应。你必须知道每个客户端都有自己的Websocket。

In your code, you are writing a response to the actually created Websocket (ie the actual Websocket between the calling client and the server). You have to know that each client has his own Websocket.

如果你想给每个客户端写一些东西,你必须在所有地方保留所有创建的Websockets。在播放示例中,已完成使用这些代码部分:

If you want to write something to each client, you have to persist all the created Websockets somewhere. In the Play sample, it is done with these parts of code:

包含所有Websockets的地图(实际有输出):

Map<String, WebSocket.Out<JsonNode>> members = new HashMap<String, WebSocket.Out<JsonNode>>();

然后注册完成这里

members.put(join.username, join.channel);

并且会向所有客户发送一条消息,该消息由遍历所有注册的Webscokets

And a message is sent to all clients, it is done by iterating through all registered Webscokets:

for(WebSocket.Out<JsonNode> channel: members.values()) {

    ObjectNode event = Json.newObject();
    event.put("kind", kind);
    event.put("user", user);
    event.put("message", text);

    ArrayNode m = event.putArray("members");
    for(String u: members.keySet()) {
        m.add(u);
    }

    channel.write(event);
}

这篇关于Websocket在playframework 2中发送所有客户端的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆