You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Za...@wistronits.com on 2012/11/23 04:05:51 UTC

two questions about websocket in tomcat7.0.30

Dears,
  My PC environment are windows XP professional Version 2002 Service Pack 3, JDK1.6, Tomcat7.0.30.
my Java web project is an internet online chat-room based on websocket. I run the local-host service in Tomcat and then connect my chat-room in chrome,
if the connection between server and browser is OK, the websocket client side function onopen() will pop up a message "open".
  But, the point is if I open several pages at same time, I can see all of pages pop up the message "open", it means every connection of page to server is OK.
but when I try to send some words to server, The server has no response ( in the other word, the server side function onTextMessage() is not called),
it happen in part of the pages not all of them. I would like to know if you encounter the same problem.
  Excuse me, the second question: how to set the maximum idle time of connection of websocket.
The set up in server.xml does not work:
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="5000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="5000"
               redirectPort="8443" />
    -->


These is part of the client side code:
function startWebSocket() {
    if ('WebSocket' in window)
        {ws = new WebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
        }
    else if ('MozWebSocket' in window)
        {ws = new MozWebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
    }
    else
        alert("not support");

    ws.onmessage = function(evt) {
        alert(evt.data);
    };
    ws.onopen = function(evt) {
        alert("open");
    };
    ws.onclose = function(evt) {
        alert("close");
    };

}
And part of the server side code:
public class MyMessageInbound extends MessageInbound {

    @Override
    protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
        // TODO Auto-generated method stub

   }

    @Override
    protected void onTextMessage(CharBuffer msg) throws IOException {
        System.out.println(msg.toString());
        for (MessageInbound messageInbound : InitServlet.getSocketList()) {
                WsOutbound outbound = messageInbound.getWsOutbound();
                CharBuffer buffer = CharBuffer.wrap(msg);
                outbound.writeTextMessage(buffer);
                outbound.flush();
        }
    }

    @Override
    protected void onClose(int status) {
        InitServlet.getSocketList().remove(this);
        super.onClose(status);
        System.out.println(getTime()+" connection is closed");
    }

    @Override
    protected void onOpen(WsOutbound outbound) {
        super.onOpen(outbound);
        InitServlet.getSocketList().add(this);
        System.out.println(getTime()+" connection is open");
    }

        public  String getTime(){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String NowTime = dateFormat.format( now );
        return NowTime;
    }

}

  I would eagerly look forward to your reply.
**********************************************************************************************************************************************
This e-mail is confidential for WistronITS corp. It may be legally privileged. If you are not the Addressee you may not copy,
forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your
 system and notify the sender immediately by return e-mail.Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any errors or omissions.
***********************************************************************************************************************************************

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


Re: 答复: two questions about websocket in tomcat7.0.30

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Zane,

On 11/26/12 6:08 AM, Zane_Zhang@wistronits.com wrote:
> The servlet is OK, and I have solved the problem.

Can you please describe what the problem was and also the solution? This
is a community of users and not a help desk. Please contribute back to
the community.

> And I would like to know how to set the maximum idle time of 
> connection of websocket, and why the function ( onclose() )on
> server side can be called in windows operating system when I unplug
> the network cable, but the it can not be called in linux operating 
> system.

Please start a new post with a new subject when you have a new question.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iEYEARECAAYFAlCzusQACgkQ9CaO5/Lv0PDfUQCgqb17oURi/u4EG1q/uC/wJRvu
N90AnjyhJRt13AfdsFMkgbEXB54v8EP1
=iQHL
-----END PGP SIGNATURE-----

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


答复: two questions about websocket in tomcat7.0.30

Posted by Za...@wistronits.com.
  The servlet is OK, and I have solved the problem. And I would like to know how to set the maximum idle time of connection of websocket, and why the function ( onclose() )on server side can be called in windows operating system when I unplug the network cable, but the it can not be called in linux operating system.

-----邮件原件-----
发件人: Pid * [mailto:pid@pidster.com]
发送时间: 2012年11月23日 16:20
收件人: Tomcat Users List
主题: Re: two questions about websocket in tomcat7.0.30

On 23 Nov 2012, at 03:06, "Zane_Zhang@wistronits.com"
<Za...@wistronits.com> wrote:

> Dears,
>  My PC environment are windows XP professional Version 2002 Service Pack 3, JDK1.6, Tomcat7.0.30.
> my Java web project is an internet online chat-room based on websocket. I run the local-host service in Tomcat and then connect my chat-room in chrome,
> if the connection between server and browser is OK, the websocket client side function onopen() will pop up a message "open".
>  But, the point is if I open several pages at same time, I can see all of pages pop up the message "open", it means every connection of page to server is OK.
> but when I try to send some words to server, The server has no response ( in the other word, the server side function onTextMessage() is not called),
> it happen in part of the pages not all of them. I would like to know if you encounter the same problem.
>  Excuse me, the second question: how to set the maximum idle time of connection of websocket.
> The set up in server.xml does not work:
> <Connector port="8080" protocol="HTTP/1.1"
>               connectionTimeout="5000"
>               redirectPort="8443" />
>    <!-- A "Connector" using the shared thread pool-->
>    <!--
>    <Connector executor="tomcatThreadPool"
>               port="8080" protocol="HTTP/1.1"
>               connectionTimeout="5000"
>               redirectPort="8443" />
>    -->
>
>
> These is part of the client side code:
> function startWebSocket() {
>    if ('WebSocket' in window)
>        {ws = new WebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
>        }
>    else if ('MozWebSocket' in window)
>        {ws = new MozWebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
>    }
>    else
>        alert("not support");
>
>    ws.onmessage = function(evt) {
>        alert(evt.data);
>    };
>    ws.onopen = function(evt) {
>        alert("open");
>    };
>    ws.onclose = function(evt) {
>        alert("close");
>    };
>
> }

How have you configured the WebSocket Servlet?


p

> And part of the server side code:
> public class MyMessageInbound extends MessageInbound {
>
>    @Override
>    protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
>        // TODO Auto-generated method stub
>
>   }
>
>    @Override
>    protected void onTextMessage(CharBuffer msg) throws IOException {
>        System.out.println(msg.toString());
>        for (MessageInbound messageInbound : InitServlet.getSocketList()) {
>                WsOutbound outbound = messageInbound.getWsOutbound();
>                CharBuffer buffer = CharBuffer.wrap(msg);
>                outbound.writeTextMessage(buffer);
>                outbound.flush();
>        }
>    }
>
>    @Override
>    protected void onClose(int status) {
>        InitServlet.getSocketList().remove(this);
>        super.onClose(status);
>        System.out.println(getTime()+" connection is closed");
>    }
>
>    @Override
>    protected void onOpen(WsOutbound outbound) {
>        super.onOpen(outbound);
>        InitServlet.getSocketList().add(this);
>        System.out.println(getTime()+" connection is open");
>    }
>
>        public  String getTime(){
>        Date now = new Date();
>        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
>        String NowTime = dateFormat.format( now );
>        return NowTime;
>    }
>
> }
>
>  I would eagerly look forward to your reply.
> **********************************************************************************************************************************************
> This e-mail is confidential for WistronITS corp. It may be legally privileged. If you are not the Addressee you may not copy,
> forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your
> system and notify the sender immediately by return e-mail.Internet communications cannot be guaranteed to be timely,
> secure, error or virus-free. The sender does not accept liability for any errors or omissions.
> ***********************************************************************************************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

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

**********************************************************************************************************************************************
This e-mail is confidential for WistronITS corp. It may be legally privileged. If you are not the Addressee you may not copy,
forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your
 system and notify the sender immediately by return e-mail.Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any errors or omissions.
***********************************************************************************************************************************************

Re: two questions about websocket in tomcat7.0.30

Posted by Pid * <pi...@pidster.com>.
On 23 Nov 2012, at 03:06, "Zane_Zhang@wistronits.com"
<Za...@wistronits.com> wrote:

> Dears,
>  My PC environment are windows XP professional Version 2002 Service Pack 3, JDK1.6, Tomcat7.0.30.
> my Java web project is an internet online chat-room based on websocket. I run the local-host service in Tomcat and then connect my chat-room in chrome,
> if the connection between server and browser is OK, the websocket client side function onopen() will pop up a message "open".
>  But, the point is if I open several pages at same time, I can see all of pages pop up the message "open", it means every connection of page to server is OK.
> but when I try to send some words to server, The server has no response ( in the other word, the server side function onTextMessage() is not called),
> it happen in part of the pages not all of them. I would like to know if you encounter the same problem.
>  Excuse me, the second question: how to set the maximum idle time of connection of websocket.
> The set up in server.xml does not work:
> <Connector port="8080" protocol="HTTP/1.1"
>               connectionTimeout="5000"
>               redirectPort="8443" />
>    <!-- A "Connector" using the shared thread pool-->
>    <!--
>    <Connector executor="tomcatThreadPool"
>               port="8080" protocol="HTTP/1.1"
>               connectionTimeout="5000"
>               redirectPort="8443" />
>    -->
>
>
> These is part of the client side code:
> function startWebSocket() {
>    if ('WebSocket' in window)
>        {ws = new WebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
>        }
>    else if ('MozWebSocket' in window)
>        {ws = new MozWebSocket("ws://localhost:8080/MyWebSocket/mywebsocket.do");
>    }
>    else
>        alert("not support");
>
>    ws.onmessage = function(evt) {
>        alert(evt.data);
>    };
>    ws.onopen = function(evt) {
>        alert("open");
>    };
>    ws.onclose = function(evt) {
>        alert("close");
>    };
>
> }

How have you configured the WebSocket Servlet?


p

> And part of the server side code:
> public class MyMessageInbound extends MessageInbound {
>
>    @Override
>    protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
>        // TODO Auto-generated method stub
>
>   }
>
>    @Override
>    protected void onTextMessage(CharBuffer msg) throws IOException {
>        System.out.println(msg.toString());
>        for (MessageInbound messageInbound : InitServlet.getSocketList()) {
>                WsOutbound outbound = messageInbound.getWsOutbound();
>                CharBuffer buffer = CharBuffer.wrap(msg);
>                outbound.writeTextMessage(buffer);
>                outbound.flush();
>        }
>    }
>
>    @Override
>    protected void onClose(int status) {
>        InitServlet.getSocketList().remove(this);
>        super.onClose(status);
>        System.out.println(getTime()+" connection is closed");
>    }
>
>    @Override
>    protected void onOpen(WsOutbound outbound) {
>        super.onOpen(outbound);
>        InitServlet.getSocketList().add(this);
>        System.out.println(getTime()+" connection is open");
>    }
>
>        public  String getTime(){
>        Date now = new Date();
>        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
>        String NowTime = dateFormat.format( now );
>        return NowTime;
>    }
>
> }
>
>  I would eagerly look forward to your reply.
> **********************************************************************************************************************************************
> This e-mail is confidential for WistronITS corp. It may be legally privileged. If you are not the Addressee you may not copy,
> forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your
> system and notify the sender immediately by return e-mail.Internet communications cannot be guaranteed to be timely,
> secure, error or virus-free. The sender does not accept liability for any errors or omissions.
> ***********************************************************************************************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

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