tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Funian Li <lifun...@gmail.com>
Subject Tomcat websocket server onclose failed( Tomcat 8.5)
Date Sun, 08 Apr 2018 12:53:53 GMT
 Dear All,

  A problem happen when tomcat websocket server was used.

   A sensor data chart with sampling freqency 50Hz is drawn. Tomcat
websocket server( Version 8.5)  is used to push the data with 20ms
interval. The server code is as following:

@ServerEndpoint("/websocketendpoint")public class WsServer {private
Session session;private boolean keepingrunning;private static
CopyOnWriteArraySet<WsServer> webSocketSet
         = new  CopyOnWriteArraySet<WsServer>();
@OnClosepublic void onClose() {
  System.out.println("Close Connection ...");
  keepingrunning = true;
  webSocketSet.remove(this);}
@OnOpenpublic void onOpen(Session session) {
   System.out.println("Open Connection ...");
   keepingrunning = true;
   this.session = session;
   webSocketSet.add(this);
 }
@OnMessagepublic void onMessage(String message, Session session) {
  System.out.println("Message from the client: " + message);
  try {
    Random r = new Random();
    // keep sending data
    while (keepingrunning) {
        int daf = r.nextInt();
        this.session.getBasicRemote().sendText(Integer.toString(daf));
        Thread.sleep(20);
     }
  } catch (Exception ex) {
    ex.printStackTrace();
 }
    System.out.println("end");
 }
@OnErrorpublic void onError(Throwable e) {
  keepingrunning = true;
  e.printStackTrace();}}

The client is as following:

var  ws=new WebSocket("ws://localhost:8080/WebSocketTest/websocketendpoint");
function start_webserver(){
       ws.send("ac_1");}

ws.onopen = function(evt) {
    console.log("Connection open ...");           };

ws.onmessage = function(evt) {
    console.log( "Received Message: " + evt.data);
    if(chart.series[0].data.length > 400) {
        chart.series[0].addPoint(parseFloat(evt.data), false, true, false);
    } else{
        chart.series[0].addPoint(parseFloat(evt.data), false, false, false);
    }};

ws.onclose = function(evt) {
    console.log("Connection closed.");
    ws.close();};


ws.onerror = function (evt) {
  console.log("error: ", evt);
 };


function end_webserver(){
   console.log("end the connection");
   ws.close();
   console.log("over");}
 // the button
 <button id="button" onclick="start_webserver()">Start</button>
 <button id="button" onclick="end_webserver()">stop</button>


Questions:

When start button clicked, the client can recieve the data to display the
chart continously. But the stop button clicked , the onclose function both
on client and server side did not call. It seem that onclose function is
blocked by the onmessage function because the server keep sending data.
Another websocket sever is tested and no problem happed. So is is the bug
of tomcat socketserver ? Thanks.

Regards,
Skyspeed

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message