Re: Tomcat websocket server onclose failed( Tomcat 8.5)

2018-04-22 Thread Funian Li
 Thank you very much for help.

Best regards,
Funian Li


2018-04-18 19:00 GMT+08:00 Mark Thomas :

> On 13/04/18 09:47, Mark Thomas wrote:
> > On 13/04/18 03:13, Funian Li wrote:
> >> 2018-04-12 4:19 GMT+08:00 Mark Thomas :
> >>
> >>> On 11/04/18 09:50, Funian Li wrote:
> >>>>  Dear All,
> >>>>
> >>>>   A problem happen when tomcat websocket server was used.
> >>>
> >>>> Exact Tomcat version?
> >>>
> >>>> If not 8.5.30, does the problem still occur if you upgrade to 8.5.30?
> >>>
> >>>> Mark
> >>>
> >>
> >>  Yes , the exact Tomcat version is 8.5.30, shall i upgrade to tomcat
> 9.0 ?
> >
> > Thanks for the version information.
> >
> > An upgrade to 9.0.x is unlikely to help. The code is almost identical
> > between 8.5.x and 9.0.x.
> >
> > The next step will be to try the test case you provided to see if the
> > problem can be reproduced.
>
> Adding the code to my (resurrected) github project for Tomcat test
> cases[1] and formatting the code made the root cause obvious.
>
> As per the Java WebSocket specification:
> 
> [WSC-5.1-1] In all cases, the implementation must not invoke an
> endpoint instance with more than one thread per peer at a time.
> 
>
> In onMessage() a loop is entered (on the container thread for that peer)
> that can never be exited because the stop message will never be
> processed because that requires a container thread and the only
> container thread allowed is processing the loop.
>
> The fix is to spwan a new thread in onMessage() and use that to write
> the messages.
>
> Mark
>
>
> [1] https://github.com/markt-asf/tomcat-bugs
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: Tomcat websocket server onclose failed( Tomcat 8.5)

2018-04-12 Thread Funian Li
2018-04-12 4:19 GMT+08:00 Mark Thomas :

> On 11/04/18 09:50, Funian Li wrote:
> >  Dear All,
> >
> >   A problem happen when tomcat websocket server was used.
>
> >Exact Tomcat version?
>
> >If not 8.5.30, does the problem still occur if you upgrade to 8.5.30?
>
> >Mark
>

 Yes , the exact Tomcat version is 8.5.30, shall i upgrade to tomcat 9.0 ?

  Thanks.

Funian Li


>
>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 webSocketSet
>  = new  CopyOnWriteArraySet();
> @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
>  Start
>  stop
>
>
> 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
>
>
> -
>


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org


Tomcat websocket server onclose failed( Tomcat 8.5)

2018-04-11 Thread Funian Li
 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 webSocketSet
 = new  CopyOnWriteArraySet();
@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
 Start
 stop


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


-


Tomcat websocket server onclose failed( Tomcat 8.5)

2018-04-08 Thread Funian Li
 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 webSocketSet
 = new  CopyOnWriteArraySet();
@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
 Start
 stop


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