简体   繁体   English

来自一个外部IP地址的WebSocket客户端

[英]WebSocket clients from one external ip address

I wrote a chat through WebSockets. 我通过WebSockets聊天。 Server side is written in Java (Tomcat) and client in JavaScript. 服务器端用Java(Tomcat)编写,客户端用JavaScript编写。

All works fine. 一切正常。 But when I try connect to server from two machines which are in local network (they're under router; have one external ip) only one client receives data from server. 但是,当我尝试从局域网中的两台计算机(它们都位于路由器下;具有一个外部IP)连接到服务器时,只有一个客户端从服务器接收数据。

Connection ( socket.onopen() ) works for both. 连接(socket.onopen())都适用。 socket.send(data) also works on both clients; socket.send(data)也可以在两个客户端上使用;

But receiving messages ( socket.onmessage() ) works just on first connected client. 但是接收消息(socket.onmessage())仅在第一个连接的客户端上有效。

How can I resolve this problem? 我该如何解决这个问题?

Problem was in server part. 问题出在服务器部分。

I wanted to broadcast incoming message to all available connections which were in ArrayList. 我想将传入消息广播到ArrayList中的所有可用连接。

After sending message to first user, received message became emptied. 向第一位用户发送消息后,收到的消息将被清空。 So, message was sent, but it was just empty string. 因此,已发送消息,但它只是空字符串。

My english is bad. 我的英语不好。 So I will whow : 所以我会:

before : 之前:

 protected void onTextMessage(CharBuffer message) throws IOException {
  // List<MessageInbound> connections
  for (MessageInbound user : connections ) 
   user.getWsOutbound.writeTextMessage(message);
 } 

after: 后:

 protected void onTextMessage(CharBuffer message) throws IOException {
  String msg = message.toString();
  for (MessageInbound user : connections ) 
   user.getWsOutbound.writeTextMessage(CharBuffer.wrap(msg));
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM