我在我的项目中使用来自codebutler的android-websockets库。但是当我执行disconnect()
方法时,我遇到了问题。以下是代码:
public void disconnectServer()
{
if(client != null)
{
try {
if(client.isConnected())
{
client.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我得到以下异常:
05-11 17:58:19.873:W / System.err(29443):java.net.SocketException: 套接字关闭
05-11 17:58:19.873:W / System.err(29443):at libcore.io.Posix.recvfromBytes(Native Method)
05-11 17:58:19.873:W / System.err(29443):at libcore.io.Posix.recvfrom(Posix.java:161)
05-11 17:58:19.873:W / System.err(29443):at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
05-11 17:58:19.878:W / System.err(29443):at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
05-11 17:58:19.878:W / System.err(29443):at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
05-11 17:58:19.878:W / System.err(29443):at java.net.PlainSocketImpl.access $ 000(PlainSocketImpl.java:37)
05-11 17:58:19.878:W / System.err(29443):at java.net.PlainSocketImpl $ PlainSocketInputStream.read(PlainSocketImpl.java:237)
05-11 17:58:19.878:W / System.err(29443):at libcore.io.Streams.readSingleByte(Streams.java:41)
05-11 17:58:19.878:W / System.err(29443):at java.net.PlainSocketImpl $ PlainSocketInputStream.read(PlainSocketImpl.java:233)
05-11 17:58:19.878:W / System.err(29443):at java.io.DataInputStream.readByte(DataInputStream.java:75)
05-11 17:58:19.878:W / System.err(29443):at com.codebutler.android_websockets.HybiParser.start(HybiParser.java:120)
05-11 17:58:19.878:W / System.err(29443):at com.codebutler.android_websockets.WebSocketClient $ 1.run(WebSocketClient.java:145)
05-11 17:58:19.878:W / System.err(29443):at java.lang.Thread.run(Thread.java:818)
请帮忙!!!!!
答案 0 :(得分:0)
这是因为disconnect()方法的实现不与connect()方法实现中创建的线程同步。 codebutler/android-websockets不具备商业品质。 disconnect()方法甚至不执行closing handshake所需的RFC 6455。
如果您不想看到错误,请使用另一个WebSocket库。