WebsocketConfig.java


@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

WebsocketServer.java


@Log4j
@Component
@ServerEndpoint("/websocket/{id}")
public class WebsocketServer {
private static Map<String, Session> connections = new HashMap<>();
private Session session;
@OnOpen
public void onOpen(Session session, @PathParam("id") String id) {
this.session = session;
connections.put(id, session);
}
@OnMessage
public void onMessage(String text){
log.info("WebSocket连接数:" + connections.size());
String[] s = text.split("]#!]");
Session ses = connections.get(s[0]);
try {
ses.getBasicRemote().sendText(s[1]);
} catch (IOException e) {
MythException.recode("websocket服务器异常:"+e.getMessage(), e, WebsocketServer.class);
}
}
@OnError
public void onError(Throwable throwable) {
throwable.printStackTrace();
log.error(throwable.getMessage());
}
@OnClose
public void onClosing() throws IOException {
connections.remove(session);
session.close();
}
}

WebsocketClient.java


@Component
@Log4j
@ClientEndpoint
public class WebSocketClient { private Session session; @OnOpen
public void onOpen(Session session) {
this.session = session;
} @OnMessage
public void onMessage(String text) throws IOException {
session.getBasicRemote().sendText(text);
} @OnError
public void onError(Throwable throwable) {
log.error(throwable.getMessage());
} @OnClose
public void onClosing() throws IOException {
// log.info("连接关闭");
session.close();
} public void sendMessage(String toId, String text) throws IOException {
text = toId + "]#!]" + text;
// log.info(text);
onMessage(text);
} public static WebSocketClient connect(String url) throws Exception {
WebSocketContainer wsc = ContainerProvider.getWebSocketContainer();
WebSocketClient client = new WebSocketClient();
wsc.connectToServer(client, new URI(url));
return client;
} }

Use.java


WebSocketClient client;
try {
client = WebSocketClient.connect("ws://wx.jjyouhuigo.com/websocket/system");
// 发送消息, id 为接受者id, text为发送的信息
client.sendMessage(id, System.currentTimeMillis()+" 文件导入有错误,请重新配置后导入");
// 关闭连接
client.onClosing();
} catch (Exception e) {
e.printStackTrace();
}

View.js


var ws = new WebSocket("ws://127.0.0.1/websocket/" + id);
ws.onopen = function () {
console.log("open");
};
ws.onmessage = function (evt) {
console.log(evt.data)
};
ws.onclose = function (evt) {
console.log("close");
};
ws.onerror = function (evt) {
console.log("error");
};

最新文章

  1. SQLite 粗劣内容
  2. 十大经典排序算法总结——JavaScrip版
  3. maven的简单说明
  4. 附加到iis进程调试时找不到w3wp.exe
  5. 毫秒数转换为指定格式日期的js代码
  6. golang的连接池例子
  7. IE10、IE11出现“__doPostBack未定义”的解决办法。
  8. Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)易错使用--转载
  9. 下载服务器dll文件并动态加载
  10. PAT-乙级-1042. 字符统计(20)
  11. SOAP Services for Python
  12. <二> ASP.NET AutoPostBack
  13. windows 批量执行命令的脚本
  14. PA 项目任务创建资源
  15. 【长期更新】迈向现代化的 .Net 配置指北
  16. echarts-饼状图默认选中高亮
  17. zstd --压缩工具
  18. 安装与配置Flutter开发环境
  19. JS with
  20. JTAG 引脚自动识别 JTAG Finder, JTAG Pinout Tool, JTAG Pin Finder, JTAG pinout detector, JTAGULATOR, Easy-JTAG, JTAG Enumeration

热门文章

  1. centos调整屏幕亮度
  2. CentOS修改Mysql字符集
  3. TCP层shutdown系统调用的实现分析
  4. [转]基于java的程序OutOfMemory问题的解决及Xms/Xmx/Xss的解释和应用
  5. 网络配置及一些shell命令概览
  6. vue问题六:表单验证
  7. 数据库开源框架之GreenDAO
  8. oracle 查看表空间使用比
  9. Proxmox
  10. flask 学习 (五)