如何在Spring中配置Websocket
1个回答
展开全部
package com.test.websocket.socket;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* spring扫描注解的配置我这就不放了,百度一下就知道了
* 这是服务端的代码。
* onOpen,onMessage,onClose三个注解分别对应请求打开,消息发送,请求关闭(跟js端一一对应的)
*/
@Component
@ServerEndpoint(value="/websocket")
public class MyWebSocket {
private static int online = 0;
private static CopyOnWriteArraySet<MyWebSocket> webSockets = new CopyOnWriteArraySet<>();
private Session session;
@OnOpen
public void onOpen(Session session){
this.session = session;
webSockets.add(this);
addCount();
JSONObject jsonObject = new JSONObject();
jsonObject.put("count",getCount());
jsonObject.put("command","init");
this.sendMessage(jsonObject);
jsonObject.put("command","add");
this.sendMessageToAll(jsonObject);
}
@OnClose
public void onClose(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("command","sub");
webSockets.remove(this);
this.sendMessageToAll(jsonObject);
subCount();
}
@OnMessage
public void onMessage(String message,Session session){
this.session = session;
JSONObject jsonObject = new JSONObject();
jsonObject.put("command","message");
jsonObject.put("content",message);
this.sendMessageToAll(jsonObject);
}
public static synchronized void addCount(){
MyWebSocket.online++;
}
public static synchronized void subCount(){
MyWebSocket.online--;
}
public static synchronized int getCount(){
return MyWebSocket.online;
}
public void sendMessage(JSONObject message){
try {
this.session.getBasicRemote().sendText(message.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessageToAll(JSONObject message){
try {
for(MyWebSocket socket : MyWebSocket.webSockets){
socket.session.getBasicRemote().sendText(message.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
var websocket = null;
var count = 0;
$('.btn-info').click(function () {
//发起websocket请求
websocket = new WebSocket("ws://localhost:8090/websocket");
$('.login_btn').hide();
$('.main').show(100);
//跟服务端onOpen执行完成之后执行
websocket.onopen = function(){
alert("链接成功");
}
//服务端onMessage执行完成之后执行
websocket.onmessage = function(event){
var data = event.data;
data = JSON.parse(data);
var command = data.command;
switch (command){
case "init" : count = data.count;$('.top').find('span').text(count);break;
case "add" : $('.top').find('span').text(++count);break;
case "sub" : $('.top').find('span').text(--count);break;
case "message" : $('.content').append("<div>" + data.content + "</div>");break;
}
}
//服务端onClose执行完成之后执行
websocket.onclose = function(){
$(".main").hide(100);
$(".login_btn").show();
$('.top').find('span').text(--count);
alert("退出成功");
}
})
$('#logout').click(function(){
//触发服务端onClose
websocket.close();
websocket = null;
})
$('.btn-success').click(function () {
//触发服务端onMessage
websocket.send($('#in').val());
})
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询