Re: 答复: [jetty-user] websocket problem

Joakim Erdfelt <[email protected]> Tue, 27 Nov 2012 06:21:35 -0700
Newsgroups gmane.comp.java.jetty.support
Message-ID <CAG4zZZCVyXdcqKUr99Vv1Vf8_p_GdXWn7e4UR4qRem7LrbpKbA@mail.gmail.com>
--f46d042ef76bdb40c004cf79ed4d
Content-Type: text/plain; charset=GB2312
Content-Transfer-Encoding: quoted-printable

YinZhang,

Java cannot determine if a connection has an issue that results in it being
closed.
(Some examples:  Remote side turned off their power. A physical layer
connection has been severed. A cable has been unplugged.  A mobile client
has moved to an area with no coverage.  The server had a network interface
failure.  A router suddenly stops sending packets to either end.)

Java can only tell if there is a failure when attempting to read or write
to that connection.  (Commonly in the form of an IOException,
TimeoutException, or SocketException)
If you do not read or write, then as far as Java is concerned, there is
nothing wrong.

Now, as far as connections under Jetty, we also have a Idle Timeout
mechanism, where if the connection hasn't had any activity within a
configured amount of time, the connection is torn down and closed
forcefully by Jetty.

--
Joakim Erdfelt <[email protected]>
webtide.com <http://www.webtide.com/>
Developer advice, services and support
from the Jetty & CometD experts
eclipse.org/jetty - cometd.org



On Tue, Nov 27, 2012 at 3:25 AM, <[email protected]> wrote:

>  *hi Brett**=A3=BA***
>
> * *
>
> *But now the problem is, unplug cable connection, and do not have access
> to the onclose ( ) method, this is a service deployment in centos6. 2, th=
e
> jetty cannot determine** whether the connection is broken?.*
>
> * *
>
> *If the service is deployed in the window xp system, unplug cable
> connection into the onclose ( ) method.***
>
>
>
>
>
> *=B7=A2=BC=FE=C8=CB:* Brett Wooldridge [mailto:[email protected]=
]
> *=B7=A2=CB=CD=CA=B1=BC=E4:* 2012=C4=EA11=D4=C227=C8=D5 18:12
> *=CA=D5=BC=FE=C8=CB:* [email protected]
> *=D6=F7=CC=E2:* Re: [jetty-user] websocket problem
>
>
>
> Yin,
>
>
>
> As far as I know, the only way to know there was a disconnection is when
> you attempt to write data to that websocket.  When a connection is
> physically unplugged, TCP does not actually disconnect the socket, becaus=
e
> such an interruption may be transient.  In jetty, if you have a class tha=
t
> implements WebSocket.OnTextMessage, for example, you can implement
> something (roughly) like this:
>
>
>
> pubic void sendMessage(String message) {
>
>    try {
>
>       connection.sendMessage(message); // send message to user's websocke=
t
> connection
>
>    } catch (IOException io) {
>
>       connection.close();
>
>    }
>
> }
>
>
>
> @Override
>
> public void onOpen(Connection connection) {
>
>    this.connection =3D connection;
>
>    // join chat room
>
> }
>
>
>
> @Override
>
> public void onClose(int closeCode, String message) {
>
>    if (connection !=3D null) {
>
>       connection =3D null;
>
>       // leave chatroom
>
>    }
>
> }
>
>
>
>
>
> On Tue, Nov 27, 2012 at 6:40 PM, <[email protected]> wrote:
>
> hi all:
>
> I am using jetty-hightide-8.0.4.v20111024, the use of websocket to make a
> chat, I encountered a problem, when three people at the same time online
> chat, when suddenly a man unplug cable out of chat, server is unable to
> determine the connection is disconnected, how the server know unplugged
> network line of human connection state? Can you help me? Thank you very
> much!
>
>
>
>       Best Regards,
>
>  Yin zhang
>
>
>
>
> *************************************************************************=
*********************************************************************
>
> 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 messag=
e
> 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.
>
>
> *************************************************************************=
**********************************************************************
>
>
>
>
> *************************************************************************=
*********************************************************************
>
> 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 messag=
e
> 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.
>
>
> *************************************************************************=
**********************************************************************
>

--f46d042ef76bdb40c004cf79ed4d
Content-Type: text/html; charset=GB2312
Content-Transfer-Encoding: quoted-printable

YinZhang,<div><br></div><div>Java cannot determine if a connection has an i=
ssue that results in it being closed.</div><div>(Some examples: &nbsp;Remot=
e side turned off their power. A physical layer connection has been severed=
. A cable has been unplugged. &nbsp;A mobile client has moved to an area wi=
th no coverage. &nbsp;The server had a network interface failure. &nbsp;A r=
outer suddenly stops sending packets to either end.)</div>
<div><br></div><div>Java can only tell if there is a failure when attemptin=
g to read or write to that connection. &nbsp;(Commonly in the form of an IO=
Exception, TimeoutException, or SocketException)</div><div>If you do not re=
ad or write, then as far as Java is concerned, there is nothing wrong.</div=
>
<div><br></div><div>Now, as far as connections under Jetty, we also have a =
Idle Timeout mechanism, where if the connection hasn&#39;t had any activity=
 within a configured amount of time, the connection is torn down and closed=
 forcefully by Jetty.</div>
<div><br clear=3D"all"><div>--</div><div>Joakim Erdfelt &lt;<a href=3D"mail=
to:[email protected]" target=3D"_blank">[email protected]</a>&gt;</div><d=
iv><a href=3D"http://www.webtide.com/" target=3D"_blank">webtide.com</a></d=
iv><div>
<span>Developer advice, services and support</span><br><span>from the Jetty=
 &amp; CometD experts</span></div><div><a href=3D"http://eclipse.org/jetty/=
" target=3D"_blank">eclipse.org/jetty</a>&nbsp;-&nbsp;<a href=3D"http://com=
etd.org/" target=3D"_blank">cometd.org</a></div>
<br>
<br><br><div class=3D"gmail_quote">On Tue, Nov 27, 2012 at 3:25 AM,  <span =
dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank=
">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex">





<div lang=3D"ZH-CN" link=3D"blue" vlink=3D"purple">
<div>
<p class=3D"MsoNormal"><b><span lang=3D"EN-US" style=3D"font-size:15.0pt;fo=
nt-family:GulimChe">hi Brett</span></b><b><span style=3D"font-size:15.0pt;f=
ont-family:SimSun">=A3=BA</span></b><b><span lang=3D"EN-US" style=3D"font-s=
ize:15.0pt;font-family:GulimChe"></span></b></p>

<p class=3D"MsoNormal"><b><span lang=3D"EN-US" style=3D"font-size:15.0pt;fo=
nt-family:GulimChe">&nbsp;</span></b></p>
<p class=3D"MsoNormal" style=3D"margin-left:12.0pt;line-height:15.0pt;verti=
cal-align:baseline">
<b><span lang=3D"EN-US" style=3D"font-size:15.0pt;font-family:GulimChe;bord=
er:none windowtext 1.0pt;padding:0cm">But now the problem is, unplug cable =
connection, and do not have access to the onclose ( ) method, this is a ser=
vice deployment in centos6. 2, the
 jetty cannot determine</span></b><b><span lang=3D"EN-US" style=3D"font-siz=
e:15.0pt;font-family:GulimChe"> whether the connection<span style=3D"border=
:none windowtext 1.0pt;padding:0cm"> is broken?.</span></span></b></p>
<p class=3D"MsoNormal" style=3D"margin-left:12.0pt;line-height:15.0pt;verti=
cal-align:baseline">
<b><span lang=3D"EN-US" style=3D"font-size:15.0pt;font-family:GulimChe">&nb=
sp;</span></b></p>
<p class=3D"MsoNormal" style=3D"text-indent:15.0pt;line-height:15.0pt;verti=
cal-align:baseline">
<b><span lang=3D"EN-US" style=3D"font-size:15.0pt;font-family:GulimChe;bord=
er:none windowtext 1.0pt;padding:0cm">If the service is deployed in the win=
dow xp system, unplug cable connection into the onclose ( ) method.</span><=
/b><b><span lang=3D"EN-US" style=3D"font-size:15.0pt;font-family:GulimChe">=
</span></b></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.5pt;font-=
family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">&nbsp;</sp=
an></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.5pt;font-=
family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">&nbsp;</sp=
an></p>
<div style=3D"border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0cm =
0cm 0cm">
<p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-family:SimSu=
n">=B7=A2=BC=FE=C8=CB<span lang=3D"EN-US">:</span></span></b><span lang=3D"=
EN-US" style=3D"font-size:10.0pt;font-family:SimSun"> Brett Wooldridge [mai=
lto:<a href=3D"mailto:[email protected]" target=3D"_blank">brett.w=
[email protected]</a>]
<br>
</span><b><span style=3D"font-size:10.0pt;font-family:SimSun">=B7=A2=CB=CD=
=CA=B1=BC=E4<span lang=3D"EN-US">:</span></span></b><span lang=3D"EN-US" st=
yle=3D"font-size:10.0pt;font-family:SimSun"> 2012</span><span style=3D"font=
-size:10.0pt;font-family:SimSun">=C4=EA<span lang=3D"EN-US">11</span>=D4=C2=
<span lang=3D"EN-US">27</span>=C8=D5<span lang=3D"EN-US">
 18:12<br>
</span><b>=CA=D5=BC=FE=C8=CB<span lang=3D"EN-US">:</span></b><span lang=3D"=
EN-US"> <a href=3D"mailto:[email protected]" target=3D"_blank">user@j=
etty.codehaus.org</a><br>
</span><b>=D6=F7=CC=E2<span lang=3D"EN-US">:</span></b><span lang=3D"EN-US"=
> Re: [jetty-user] websocket problem</span></span></p>
</div><div><div class=3D"h5">
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US">Yin,</span></p>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">As far as I know, the only way =
to know there was a disconnection is when you attempt to write data to that=
 websocket. &nbsp;When a connection is physically unplugged, TCP does not a=
ctually disconnect the socket, because such
 an interruption may be transient. &nbsp;In jetty, if you have a class that=
 implements WebSocket.OnTextMessage, for example, you can implement somethi=
ng (roughly) like this:</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">pubic void sendMessage(String m=
essage) {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;try {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp; &nbsp; connection=
.sendMessage(message); // send message to user&#39;s websocket connection</=
span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;} catch (IOExcepti=
on io) {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp; &nbsp; connection=
.close();</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;}</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">}</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">@Override</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">public void onOpen(Connection c=
onnection) {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;this.connection =
=3D connection;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;// join chat room<=
/span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">}</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">@Override</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">public void onClose(int closeCo=
de, String message) {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;if (connection !=
=3D null) {</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp; &nbsp; connection=
 =3D null;</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp; &nbsp; // leave c=
hatroom</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp; &nbsp;}</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">}</span></p>
</div>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<div>
<p class=3D"MsoNormal" style=3D"margin-bottom:12.0pt"><span lang=3D"EN-US">=
&nbsp;</span></p>
<div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">On Tue, Nov 27, 2012 at 6:40 PM=
, &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">YinZhang=
@wistronits.com</a>&gt; wrote:</span></p>
<div>
<div>
<p><span lang=3D"EN-US">hi all:</span></p>
<p><span lang=3D"EN-US">I am using jetty-hightide-8.0.4.v20111024, the use =
of websocket to make a chat, I encountered a problem, when three people at =
the same time online chat, when suddenly a man unplug cable out of chat, se=
rver is unable to determine the connection
 is disconnected, how the server know unplugged network line of human conne=
ction state? Can you help me? Thank you very much!</span></p>
<p><span lang=3D"EN-US">&nbsp;</span></p>
<p><span lang=3D"EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Best Regards,</span>=
</p>
<p><span lang=3D"EN-US">&nbsp;Yin zhang</span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt">*****=
***************************************************************************=
**************************************************************</span><span =
lang=3D"EN-US"></span></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">This e-mail is confidenti=
al for WistronITS corp. It may be legally privileged. If you are not the Ad=
dressee you may not copy,
</span><span lang=3D"EN-US"></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">forward, disclose or use =
any part of it. If you have received this message in error, please delete i=
t and all copies from your</span><span lang=3D"EN-US"></span></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">&nbsp;system and notify t=
he sender immediately by return e-mail.Internet communications cannot be gu=
aranteed to be timely,
</span><span lang=3D"EN-US"></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">secure, error or virus-fr=
ee. The sender does not accept liability for any errors or omissions.</span=
><span lang=3D"EN-US"></span></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt">*****=
***************************************************************************=
***************************************************************</span><span=
 lang=3D"EN-US"></span></p>

</div>
</div>
<p class=3D"MsoNormal"><span lang=3D"EN-US">&nbsp;</span></p>
</div>
</div></div></div><div><div class=3D"h5">
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;\0065b0\007d30\00660e\009ad4&quot;,&quot;serif&quot;">*********=
***************************************************************************=
**********************************************************</span></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">This e-mail is confidenti=
al for WistronITS corp. It may be legally privileged. If you are not the Ad=
dressee you may not copy,
</span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">forward, disclose or use =
any part of it. If you have received this message in error, please delete i=
t and all copies from your</span></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">&nbsp;system and notify t=
he sender immediately by return e-mail.Internet communications cannot be gu=
aranteed to be timely,
</span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;Verdana&quot;,&quot;sans-serif&quot;">secure, error or virus-fr=
ee. The sender does not accept liability for any errors or omissions.</span=
></p>

<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:8.0pt;font-f=
amily:&quot;\0065b0\007d30\00660e\009ad4&quot;,&quot;serif&quot;">*********=
***************************************************************************=
***********************************************************</span></p>

</div></div></div>

</blockquote></div><br></div>

--f46d042ef76bdb40c004cf79ed4d--