主程序按下面写:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//timer.schedule(task, 0, 200);
URI uri = URI.create("ws://sq-test.icv-cloud.com:5000/。。。。。。。。");
System.out.println(uri.toString());
MyWebSocketClient myClient = new MyWebSocketClient(uri){
@Override
public void onMessage(String message) {
Log.e("MyWebClientService", message); //message就是接收到的消息
}
};
try {
myClient.connectBlocking();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (myClient != null && myClient.isOpen()) {
myClient.send("123");
}
}
另建一个类按如下:
public class MyWebSocketClient extends WebSocketClient {
public MyWebSocketClient(URI serverUri) {
super(serverUri,new Draft_6455());}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.e("MyWebSocketClient", "onOpen()");
}
@Override
public void onMessage(String message) {
Log.e("MyWebSocketClient", "onMessage()");
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.e("MyWebSocketClient", "onClose()");
}
@Override
public void onError(Exception ex) {
Log.e("MyWebSocketClient", "onError()");
}
打印出来的日志如下:
E/MyWebSocketClient: onClose()
按道理能打印出来:具体的messa的内容的?
是Websocket没有通吗?