1、首先在pom文件中引入Webscoekt的依赖

<!-- websocket依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

2、通过注解方式构建Websocket服务

package com.kk.server.chat.websocket;

import com.kk.server.chat.service.CheckTokenService;
import com.kk.server.chat.service.QueryService;
import com.kk.server.chat.service.SaveMongodbService;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* Created by yxl on 2018-03-19.
*/
@ServerEndpoint("/chat/{userId}") //关键注解,将WebSocket服务端运行在 ws://[Server端IP或域名]:[Server端口]/websockets/chat/{userId} 的访问端点,客户端浏览器已经可以对 WebSocket客户端API发起HTTP长连接了。
@Component
public class ChatWebsocket { private Logger logger = Logger.getLogger(ChatWebsocket.class); private static ApplicationContext applicationContext; private SaveMongodbService saveMongodbService; private CheckTokenService checkTokenService; private QueryService queryService; //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
private static Map<String, Session> webSocketSet = new ConcurrentHashMap<>(); //医生web //解决Websocket不能注入bean的问题
public static void setApplicationContext(ApplicationContext applicationContext) {
ChatWebsocket.applicationContext = applicationContext;
} /**
* 连接建立成功调用的方法
*
* @param userId 用户链接ID
*/
@OnOpen //建立连接的注解,当用户建立连接时SpringBoot会监听到,然后会调用该注解下的方法
public void onOpen(@PathParam("userId") String userId, Session session) {
webSocketSet.put(userId, session);
} /**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(Session session) {
logger.info("进入关闭方法!");
if (session != null) {
Map<String, String> pathParameters = session.getPathParameters();
String userId = pathParameters.get("userId"); //从当前关闭session中获取用户ID,前提是建立websocket连接时带有userId,即 @ServerEndpoint("/chat/{userId}")
webSocketSet.remove(userId); //从map中移除用户
}
} /**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息
* @param session 可选的参数
*/
@OnMessage
public void onMessage(String message, Session session) throws IOException { try {
JSONObject jsonObject = JSONObject.parseObject(message);
String receiver_id = jsonObject.getString("receiver_id") //接受者ID,通过该Id来获取接受者的session
Session session = webSocketSet.get("receiver_id")
if (session != null) {
session.getBasicRemote.sendText(message); //通过session发送消息
}else {
//TODO 接收方不在线,处理离线消息 }
}catch(IOException e) {
e.printStackTrace;
} } /**
* 发生错误时调用
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
//onClose();
if (session != null) {
Map<String, String> pathParameters = session.getPathParameters();
String userId = pathParameters.get("userId"); //从当前关闭session中获取用户ID,前提是建立websocket连接时带有userId,即 @ServerEndpoint("/chat/{userId}")
webSocketSet.remove(userId); //从map中移除用户
}
logger.info("webscoket发生错误!"+error.getMessage());
error.printStackTrace();
}
}

3、前台调用方法(js)

function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("您的浏览器支持 WebSocket!"); // 打开一个 web socket
    var ws = new WebSocket("ws://服务器IP:端口/chat/123");
ws.onopen = function() {
// Web Socket 已连接上,使用 send() 方法发送数据
ws.send('{"userId":"1555855","message" : "test"}');
ws.close("666666");
alert("数据发送中...");
};
      
ws.onmessage = function (evt) {
console.log(evt)
alert(evt.data);
alert("数据已接收...");
}; ws.onclose = function(){
// 关闭 websocket
alert("连接已关闭...");
};
}else {
// 浏览器不支持 WebSocket
alert("您的浏览器不支持 WebSocket!");
}
}

4、当然,在使用websocket聊天时,可能会遇到一些数据需要查询数据库或者Redis,但是在websocket中又不能直接注入相应的Bean实例,这个时候可以看我的另一篇博客https://www.cnblogs.com/Amaris-Lin/p/9038813.html

Spring Boot中使用Websocket搭建即时聊天系统的更多相关文章

  1. 在spring boot中使用webSocket组件(二)

    该篇演示如何使用websocket创建一对一的聊天室,废话不多说,我们马上开始! 一.首先先创建前端页面,代码如下图所示: 1.login.html <!DOCTYPE html> < ...

  2. 在spring boot中使用webSocket组件(一)

    最近在项目中使用到了spring的webSocket组件,在这里和大家分享下,如有错误,欢迎大家指正. 在这里我使用的IDE工具是Intellij idea,框架是spring boot.spring ...

  3. Spring Boot 中使用 WebSocket 实现一对多聊天及一对一聊天

    为什么需要WebSocket? 我们已经有了http协议,为什么还需要另外一个协议?有什么好处? 比如我想得到价格变化,只能是客户端想服务端发起请求,服务器返回结果,HTTP协议做不到服务器主动向客户 ...

  4. spring boot / cloud (十七) 快速搭建注册中心和配置中心

    spring boot / cloud (十七) 快速搭建注册中心和配置中心 本文将使用spring cloud的eureka和config server来搭建. 然后搭建的模式,有很多种,本文主要聊 ...

  5. Spring Boot 中的静态资源到底要放在哪里?

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...

  6. (转)Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门

    http://www.ityouknow.com/springboot/2019/02/12/spring-boot-webflux.html Spring 5.0 中发布了重量级组件 Webflux ...

  7. Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门

    Spring 5.0 中发布了重量级组件 Webflux,拉起了响应式编程的规模使用序幕. WebFlux 使用的场景是异步非阻塞的,使用 Webflux 作为系统解决方案,在大多数场景下可以提高系统 ...

  8. 学习Spring Boot:(二十三)Spring Boot 中使用 Docker

    前言 简单的学习下怎么在 Spring Boot 中使用 Docker 进行构建,发布一个镜像,现在我们通过远程的 docker api 构建镜像,运行容器,发布镜像等操作. 这里只介绍两种方式: 远 ...

  9. 徒手撸一个 Spring Boot 中的 Starter ,解密自动化配置黑魔法!

    我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...

随机推荐

  1. python random 随机选择操作

    # -*- coding:utf-8 -*- import random arr = ['A','B','C','D','E','F'] #生成(0.0, 1.0)的随机数 print random. ...

  2. Access restriction: The type Resource is not accessible due to restriction on required library

    方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated and restri ...

  3. 学习git最好的方式

    1:登陆git官网网站 https://git-scm.com 2:点击esay to learn连接 3:点击Book连接 4:选择简体中文,下载PDF文档,也可以在线学习.

  4. linux查看某个目录下有哪些文件的命令

    分别是ll和ls命令 ll /usr/local/lib ls /usr/local/lib

  5. maven package 命令报:-source1.3 中不支持注释错误

    在使用maven 打包或者编译时报:-source1.3 中不支持注释错误解决方案如下: <build>  <plugins>   <plugin>    < ...

  6. spark-submit 提交Application

    在spark1.0中推出spark-submit来统一提交applicaiton ./bin/spark-submit \ --class <main-class> --master &l ...

  7. 【RL前沿】深度强化学习的最新进展 by 2017.12.12

    作者:Volodymyr Mnih Google DeepMind科学家. 在Geoffrey Hinton的指导下完成了多伦多大学的机器学习博士学位. 在此之前,在Csab Szepesvari的指 ...

  8. Web 跨域请求

    在前端开发过程中,难免和服务端产生数据交互.一般情况我们的请求分为这么几种情况:   1. 只关注发送,不关注接收  2.不仅要发送,还要关注服务端返回的信息       a.  同域请求      ...

  9. a标签解析url

    var url = 'http://127.0.0.1:8080/index.jsp?username=admin#name'; var aLink = document.createElement( ...

  10. JS - 函数,Math,number

    函数分为:关键字function 匿名函数 — 没有名字的函数 有名函数 — 有名字的函数 <body> <div></div> <script> // ...