1、参考链接
https://www.bilibili.com/video/BV1r54y1D72U?from=search&seid=2193943377005329491&spm_id_from=333.337.0.0
2、注解的方式
服务端代码实现=
package com.zhangz.innokproject.config.websocket;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Component
@ServerEndpoint(value = "/websocket",configurator = GetHttpInfosConfigurator.class)
public class DemoForWebsocket {
private static Map<String,DemoForWebsocket> onlineClients = new ConcurrentHashMap<>();
public static Map<String, DemoForWebsocket> getOnlineClients() {
return onlineClients;
}
private Session session;
private String client;
@OnOpen
public void onOpen(Session session,EndpointConfig config) throws Exception{
this.session = session;
}
@OnClose
public void onClose() throws Exception{
session.close();
onlineClients.remove(client);
log.info("离线客户端:"+client+"\n在线客户端:"+onlineClients.toString());
}
@OnMessage
public void onMessage(String message, Session session) throws IOException {
try {
HashMap<String,String> map = JSONObject.parseObject(message, HashMap.class);
String machine = map.get("machine");
String type = map.get("type");
log.info("==========="+type+"==========="+machine);
this.client = type+"-"+machine;
onlineClients.put(this.client,this);
log.info("在线客户端:"+onlineClients.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@OnError
public void onError(Session session, Throwable error){
error.printStackTrace();
}
public void sendMessage(String message) throws IOException{
this.session.getBasicRemote().sendText(message);
}
}
3、配置websocket
package com.zhangz.innokproject.config.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebsocketConfig {
@Bean
public ServerEndpointExporter getEndpointExporter(){
return new ServerEndpointExporter();
}
}
##4、 EndpointConfig配置类
package com.zhangz.innokproject.config.websocket;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;
import java.util.Map;
public class GetHttpInfosConfigurator extends ServerEndpointConfig.Configurator {
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
Map<String, List<String>> parameterMap = request.getParameterMap();
sec.getUserProperties().putAll(parameterMap);
super.modifyHandshake(sec, request, response);
}
}
5、定时推送客户端
package com.hlxd.innokproject.config.websocket;
import com.alibaba.fastjson.JSONObject;
import com.hlxd.innokproject.bean.MachineQuotoGroup;
import com.hlxd.innokproject.service.QuotoMonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
@Service
@EnableScheduling
@Slf4j
public class WebSocketService {
@Resource
private QuotoMonitorService quotoMonitorService;
@Scheduled(cron = "0 */1 * * * ?")
public void getMachineInfo(){
MachineQuotoGroup machineQuotoGroup = null;
try {
Map<String, DemoForWebsocket> onlineClients = DemoForWebsocket.getOnlineClients();
log.info("====onlineClients===="+onlineClients.size());
if (onlineClients.isEmpty()) return;
Set<String> set = onlineClients.keySet();
for (String key : set){
onlineClients.get(key).sendMessage(JSONObject.toJSONString(machineQuotoGroup));
}
} catch (Exception e) {
log.info("====websocket=====异常=====");
e.printStackTrace();
}
}
}
6、websocket在线工具
http://oktools.net/websocket
|