// 初始化webSocket(必须)
initWebSocket() {
//初始化webSocket(必须) ws://127.0.0.1:58080/webSocket
console.log(localStorage.getItem("userId"), "loginnnnnnnnnnnnnnn");
const socket = new WebSocket(
"ws://192.168.0.112:9205/websocket/" + localStorage.getItem("userId")
);
socket.addEventListener("open", (event) => {
// 在握手阶段添加Authorization头
socket.send("Authorization: Bearer " + YOUR_TOKEN);
});
socket.addEventListener("message", (event) => {
// 处理接收到的消息
console.log("Received:", event.data);
});
},```
webSocket 连接后端
于 2023-12-21 14:49:07 首次发布