播放2.5 WebSocket连接构建 [英] Play 2.5 WebSocket Connection Build

查看:103
本文介绍了播放2.5 WebSocket连接构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EU West上运行着一个AWS服务器(中等),大约连接了250台设备,但由于互联网连接问题,它们也总是重新连接,但由于某种原因,与服务器的TCP连接数量一直增加达到大约4300.然后不允许新连接到服务器.我已经确认它是隔离于WebSocket请求而不是常规HTTP请求的.

I have an AWS server (medium) running in EU West, and there are roughly 250 devices connected but are also always reconnecting due to internet connectivity issues, but for some reason, the amount of TCP connections to the server grows until it reaches around 4300. Then no new connections are allowed to the server. I have confirmed that it is isolated to WebSocket requests and not regular HTTP requests.

WebSocket连接在设备中以设备UUID为键的每个设备保留;有时,即使服务器与该设备建立了连接,设备仍会发送新的WS连接请求.在这种情况下,当前连接将关闭,并返回错误,以便设备可以重试连接请求.

WebSocket connections are kept per device in a Map with device UUID as key; it sometimes happens that a device will send a request for a new WS connection even though the server has a connection to the device. In this case, the current connection is closed, and an error is returned so that the device can retry the connection request.

下面是Controller使用LegacyWebSocket处理连接的代码片段.根据 https://,使用out.close()关闭连接www.playframework.com/documentation/2.5.x/JavaWebSockets#handling-websockets-using-callbacks

Below is the code snippet from the Controller handling the connections using LegacyWebSocket. Connections are closed using out.close() as per https://www.playframework.com/documentation/2.5.x/JavaWebSockets#handling-websockets-using-callbacks

public LegacyWebSocket<String> create(String uuid) {
    logger.debug("NEW WebSocket request from {}, creating new socket...", uuid);

    if(webSocketMap.containsKey(uuid)){
        logger.debug("WebSocket already exists for {}, closing existing connection", uuid);

        webSocketMap.get(uuid).close();

        logger.debug("Responding forbidden to force WS restart from device {}", uuid);
        return WebSocket.reject(forbidden());
    }

    LegacyWebSocket<String> ws = WebSocket.whenReady((in, out) -> {
        logger.debug("Adding downstream connection to webSocketMap-> {} webSocketMap.size() = {}",uuid, webSocketMap.size());
        webSocketMap.put(uuid,out);
        // For each event received on the socket,
        in.onMessage(message->{

            if(message.equals("ping")){
                logger.debug("PING received from {} {}",uuid, message);
                out.write("pong");
            }
        });


        // When the socket is closed.
        in.onClose(() -> {
            logger.debug("onClose, removing for {}",uuid);

            webSocketMap.remove(uuid);
        });
    });

    return ws;
}

如何确保Play框架为已关闭的WS连接关闭TCP连接?

How can I ensure that Play Framework closes the TCP connection for closed WS connections?

我用来检查TCP连接数量的呼叫是netstat -n -t | wc -l

The call that I use to check the amount of TCP connections is netstat -n -t | wc -l

推荐答案

看起来像TCP保持活动问题-即TCP连接由于客户端的连接问题而变得陈旧,服务器无法处理或清理在达到限制之前及时建立过时的连接.

Looks like a TCP keep-alive issue - i.e. that the TCP connections become stale because of connectivity issues on the client side and the server does not handle or clean up the stale connections in time before the limit is reached.

此链接将帮助您在服务器上配置TCP保持活动状态,以确保清除陈旧的连接及时.

This link will help you configure the TCP keep-alive on your server to ensure that the stale connections are cleaned up in time.

这篇关于播放2.5 WebSocket连接构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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