在build.gradle里引入依赖:
implementation 'org.java-websocket:Java-WebSocket:1.5.2'
在Androidmanifest.xml 文件里加入网络权限:
<uses-permission android:name="android.permission.INTERNET" />
代码:
package com.xmkjsoft.video_call;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URISyntaxException;
public class MainActivity extends AppCompatActivity {
private MyWebSocketClient webSocketClient;
// 默认无参数构造函数
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectWebSocket();
}
private void connectWebSocket() {
try {
webSocketClient = new MyWebSocketClient("ws://192.168.28.218/ws/1233");
webSocketClient.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
// 内部类,用于处理 WebSocket 连接状态和消息
private class MyWebSocketClient extends WebSocketClient {
public MyWebSocketClient(String serverUri) throws URISyntaxException {
super(new java.net.URI(serverUri));
}
@Override
public void onOpen(ServerHandshake handshakedata) {
// WebSocket 连接已打开
System.out.println("WebSocket 连接已打开");
}
@Override
public void onMessage(String message) {
// 收到文本消息
System.out.println("收到文本消息:" + message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
// WebSocket 连接已关闭
System.out.println("WebSocket 连接已关闭,code:" + code + ", reason:" + reason + ", remote:" + remote);
}
@Override
public void onError(Exception ex) {
// WebSocket 连接出错
System.out.println("WebSocket 连接出错:" + ex.getMessage());
}
}
}
服务端代码:
package com.xmkjsoft.video_call_server.websocket;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@CrossOrigin
@ServerEndpoint(value = "/ws/{userId}")
@Component
public class OnlineWebSocket {
private Session session;
private String userId;
private static Set<OnlineWebSocket> onlineWebSocketSet = new CopyOnWriteArraySet<>();
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.session = session;
this.userId = userId;
onlineWebSocketSet.add(this);
System.out.println("WebSocket 连接已建立,用户ID:" + userId);
}
@OnClose
public void onClose(CloseReason reason) {
onlineWebSocketSet.remove(this);
System.out.println("WebSocket 连接已关闭,用户ID:" + userId);
if (reason.getReasonPhrase().equals("EOFException")) {
// 处理 EOFException 异常
// 例如:重新建立连接或者通知用户连接已断开
System.out.println("连接关闭原因:EOFException");
}
}
@OnMessage
public void onMessage(Session session, String message, @PathParam("userId") String userId) {
this.session = session;
this.userId = userId;
if (!message.equals("ping")) {
System.out.println("收到客户端,用户ID:" + userId + ",消息内容:" + message);
} else {
// 收到心跳回应,不做任何处理
}
}
}