1.配置springboot支持websocket
package com.example.demo.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* websocket配置
* @author jiajia
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
2.websocket服务
package com.example.demo.websocket;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
/**
* springboot集成websocket
* @author jiajia
*/
@ServerEndpoint("/websocket/{id}")
@Component
public class WebSocketServer {
public static final Object lock=new Object();
/**
* 记录在线人数
*/
private static int onlineCount = 0;
/**
* WebSocketServer对象集合
*/
private static final Map<String,WebSocketServer> webSocketMap = new HashMap<String,WebSocketServer>();
/**
* 回话
*/
private Session session;
/**
* id标识
*/
private String id;
private AStar astar;
/**
* 连接成功时调用
* @param session
* @param sid
*/
@OnOpen
public void onOpen(Session session,@PathParam("id") String id) {
this.session = session;
putObj(id);
addOnlineCount();
try {
sendMessage("{type:1,msg:'连接成功'}");
} catch (IOException e){
}
}
/**
* 连接关闭时调用
*/
@OnClose
public void onClose() {
delObj();
subOnlineCount();
System.out.println("删除"+this.id);
}
/***
* 接受客户端发送消息时调用
* @param message 消息
* @param session
*/
@OnMessage
public void onMessage(String message, Session session) {
try {
Map<String,Object> map=JSON.parseObject(message);
System.out.println(map.get("type"));
if(map.containsKey("type")) {
if(Integer.valueOf(map.get("type").toString()).equals(1)) {//请求地图
astar = new AStar(this,Integer.valueOf(map.get("wid").toString()));
String s=JSON.toJSONString(astar.NODES);
sendMessage("{type:6,msg:"+s+"}");
System.out.println("-");
}else if(Integer.valueOf(map.get("type").toString()).equals(2)){//修改地图
int x=Integer.valueOf(map.get("x").toString());
int y=Integer.valueOf(map.get("y").toString());
if(this.astar.NODES[x][y]==0) {
this.astar.NODES[x][y]=1;
}else {
this.astar.NODES[x][y]=0;
}
}else if(Integer.valueOf(map.get("type").toString()).equals(3)){
System.out.println(map);
int startX=Integer.valueOf(map.get("startX").toString());
int startY=Integer.valueOf(map.get("startY").toString());
int endX=Integer.valueOf(map.get("endX").toString());
int endY=Integer.valueOf(map.get("endY").toString());
this.astar.begin(startX, startY, endX, endY);
}
}
sendMessage("{type:2,msg:'收到消息'}");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 给某个指定id发送消息
* @param id
* @return
*/
public boolean sendMessageById(String id,String message) {
WebSocketServer obj=webSocketMap.get(id);
if(obj!=null) {
try {
obj.sendMessage(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}else {
return false;
}
}
/**
* 群发消息
* @param message
* @return
*/
public void sendMessageGroup(String message) {
synchronized (lock) {
for (Map.Entry<String,WebSocketServer> entry : webSocketMap.entrySet()) {
try {
sendMessage(entry.getValue().session, message);
} catch (IOException e) {
}
}
}
}
/**
* 连接发生异常时调用
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
System.out.println("链接异常");
delObj();
subOnlineCount();
error.printStackTrace();
}
/**
* 服务器向本次连接的前端发送消息
* @param message
* @throws IOException
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 向某链接发送消息
* @param session
* @param message
* @throws IOException
*/
private void sendMessage(Session session,String message) throws IOException {
session.getBasicRemote().sendText(message);
}
/**
* 向webSocketMap集合中添加此对象的引用
* @param id
*/
private void putObj(String id) {
synchronized (lock){
if(!webSocketMap.containsKey(id)) {
webSocketMap.put(id,this);
this.id=id;
System.out.println(id+"链接成功:"+getOnlineCount());
}
}
}
/**
* 从webSocketMap中删除对本对象的引用
*/
private void delObj() {
synchronized (lock){
webSocketMap.remove(this.id);
}
}
/**
* 获取在线人数
* @return
*/
public static synchronized int getOnlineCount() {
return onlineCount;
}
/**
* 在线人数+1
*/
private static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
/**
* 在线人数-1
*/
private static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}