很高兴我下载了精简聊天室demo并在win7通过letnet测试通过.
然后我在安卓端使用WebSocketClient类进行connect.服务器可以接收连接,但没有建立长链接,我不知道问题在哪里.
以下工具类继承WebSocketClient类
public class JWebSocketClient extends WebSocketClient {
public JWebSocketClient(URI serverUri) {
super(serverUri, new Draft_6455());
}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.e("JWebSocketClient", "onOpen()");
}
@Override
public void onMessage(String message) {
Log.e("JWebSocketClient", "onMessage()");
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.e("JWebSocketClient", "onClose()");
}
@Override
public void onError(Exception ex) {
Log.e("JWebSocketClient", "onError()");
}
}
以下片段为调用
public static JWebSocketClient LongLink;
URI Host=URI.create("ws://192.168.1.218:8282");
Button SendButt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
SendButt = (Button) findViewById(R.id.button2);
SendButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LongLink.onMessage("android send");
}
});
}
public void init()
{
LongLink = new JWebSocketClient(Host);
try {
LongLink.connect();
}catch (IllegalStateException e)
{
Log.e("初始化失败",e.getMessage());
}
}
调用结果中 new JWebSocketClient(Host);是成功的,服务器有收到该请求,但是于init方法结束时服务端会提示logout详情如下
7f0000010b5400000007 login
7f0000010b5400000007 said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: jHn0+g9NwU4RxcQlWXrl6g==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b5400000007 logout
7f0000010b5400000008 login
7f0000010b5400000008 said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: 71q5vwZpn+5ic/D67moomA==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b5400000008 logout
7f0000010b5400000009 login
7f0000010b5400000009 said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: il7oai1ufDz3s0pHtNRwgg==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b5400000009 logout
7f0000010b540000000a login
7f0000010b540000000a said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: Cs1fF2gdf5HWC3Mw3AP/VQ==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b540000000a logout
7f0000010b540000000b login
7f0000010b540000000b said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: g0nvffYDoDZm+SA9X3QxhQ==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b540000000b logout
7f0000010b540000000c login
7f0000010b540000000c said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: M/0dh+eIY6hFi2ox9R8qag==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b540000000c logout
7f0000010b540000000d login
7f0000010b540000000d said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: XcqpPdKZCS6OpauGFp+zxw==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b540000000d logout
7f0000010b540000000e login
7f0000010b540000000e said GET / HTTP/1.1
Connection: Upgrade
Host: 192.168.1.218:8282
Sec-WebSocket-Key: xmfGuxjfBTlPtI3c8Mhsrg==
Sec-WebSocket-Version: 13
Upgrade: websocket
7f0000010b540000000e logout
客户端和服务端协议要一致才能通讯。
websocket协议的话服务端start_gateway.php里
tcp改成websocket
建议多看手册,手册有说这些
问题已解决,感谢您的关注及回复!刚接触GatewayWorker,处于测试可用性阶段,所以没有详细查看手册.我会详细查看的.