简体   繁体   中英

WebSocket clients from one external ip address

I wrote a chat through WebSockets. Server side is written in Java (Tomcat) and client in 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.

Connection ( socket.onopen() ) works for both. socket.send(data) also works on both clients;

But receiving messages ( socket.onmessage() ) works just on first connected client.

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.

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));
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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