搭建服务器框架
客户端
sendMessage() {
let message = {
userId: this.user.userId,
nickName: this.user.nickName,
channelId: this.curChannelId,
content: this.curInputMessage,
}
this.websocket.send(JSON.stringify(message));
console.log("send success! " + message.content);
this.curInputMessage = "";
},
initWebSocket() {
if('WebSocket' in window){
this.websocket = new WebSocket("ws://localhost:8080/java_chatroom/message/" + this.user.userId);
console.log("link success")
}else{
alert('Not support websocket')
}
this.websocket.onerror = function () {
alert("连接发生错误!");
};
this.websocket.onopen = function (event) {
console.log("连接建立成功");
}
this.websocket.onmessage = function (event) {
console.log