我在java中做过一个聊天web-socket应用程序服务器,我也创建了一个小型的android聊天应用程序。
import android.util.Log;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class MyWebSocketClient {
private WebSocketClient mWebSocketClient;
public void connect_to_server() {
URI uri;
try {
uri = new URI("ws://172.16.0.146:8082/PChat/chat");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
Map<String, String> headers = new HashMap<>();
mWebSocketClient = new WebSocketClient(uri, new Draft_17(), headers, 0) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i("Websocket", "Opened");
// mWebSocketClient.send("Hello from websocket client");
}
@Override
public void onMessage(String s) {
Log.i("Websocket", "onMessage"+s);
}
@Override
public void onClose(int code, String s, boolean b) {
Log.i("Websocket", code + ": Closed " + s);
}
@Override
public void onError(Exception e) {
Log.i("Websocket", "Error " + e.getMessage());
}
};
mWebSocketClient.connect();
}
public void sendMessage(String message) {
mWebSocketClient.send(message);
}
}
这是用于连接到web-socket的代码。连接正在发生,但在第二个连接中正在下垂。
01-08 10:53:10.900 1843-1843/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pchat.nanobi.pchat, PID: 1843
org.java_websocket.exceptions.WebsocketNotConnectedException
at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:566)
at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:543)
at org.java_websocket.client.WebSocketClient.send(WebSocketClient.java:171)
at com.pchat.nanobi.pchat.MainActivity.sendMessage(MainActivity.java:109)
at com.pchat.nanobi.pchat.MainActivity$2.onClick(MainActivity.java:58)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
如何从android连接到web-socket服务器?
答案 0 :(得分:0)
使用依赖:Java-WebSocket-1.3.0.jar
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
public class WebsocketClientExample {
private WebSocketClient mWebSocketClient;
public void connect_to_server() {
URI uri;
try {
uri = new URI("ws://path/to/your/websocket/endpoint");
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
Map<String, String> headers = new HashMap<>();
mWebSocketClient = new WebSocketClient(uri, new Draft_17(), headers, 0) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
Log.i("Websocket", "Opened");
mWebSocketClient.send("Hello from websocket client");
}
@Override
public void onMessage(String s) {
Log.i("Websocket", "onMessage"+s);
}
@Override
public void onClose(int code, String s, boolean b) {
Log.i("Websocket", code + ": Closed " + s);
}
@Override
public void onError(Exception e) {
Log.i("Websocket", "Error " + e.getMessage());
}
};
mWebSocketClient.connect();
}
}