实现微服务
1、vue前端如何创建WebSocket
const socketUrl = `ws://127.0.0.1:3000/websocket/${store.state.user.token}/`;
onMounted(()=>{
socket = new WebSocket(socketUrl); // WebSocket是js自带的
socket.onopen = ()=>{
console.log("connected!");
}
socket.onmessage = (msg) =>{
const data = JSON.parse(msg.data);
console.log(data);
}
socket.onclose = ()=>{
console.log("disconnected");
}
});
onUnmounted(()=>{
socket.close();
});
// 给后端发送一个字符串的api
store.state.pk.socket.send(JSON.stringify({
event: "start-matching",
}))