1、springboot2.0集成webSocket
项目结构
maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- websocket dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
核心代码
@ServerEndpoint(value = "/ws/asset")
@Component
public class WebSocketServer {
@PostConstruct
public void init() {
System.out.println("websocket 加载");
}
private static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
private static final AtomicInteger OnlineCount = new AtomicInteger(0);
// concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。
private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<Session>();
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session) {
SessionSet.add(session);
int cnt = OnlineCount.incrementAndGet(); // 在线数加1
log.info("有连接加入,当前连接数为:{}", cnt);
SendMessage(session, "连接成功");
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose(Session session) {
SessionSet.remove(session);
int cnt = OnlineCount.decrementAndGet();
log.info("有连接关闭,当前连接数为:{}", cnt);
}
/**
* 收到客户端消息后调用的方法
*
* @param message
* 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("来自客户端的消息:{}",message);
SendMessage(session, "收到消息,消息内容:"+message);
}
/**
* 出现错误
* @param session
* @param error
*/
@OnError
public void one rror(Session session, Throwable error) {
log.error("发生错误:{},Session ID: {}",error.getMessage(),session.getId());
error.printStackTrace();
}
/**
* 发送消息,实践表明,每次浏览器刷新,session会发生变化。
* @param session
* @param message
*/
public static void SendMessage(Session session, String message) {
try {
session.getBasicRemote().sendText(String.format("%s (From Server,Session ID=%s)",message,session.getId()));
} catch (IOException e) {
log.error("发送消息出错:{}", e.getMessage());
e.printStackTrace();
}
}
/**
* 群发消息
* @param message
* @throws IOException
*/
public static void BroadCastInfo(String message) throws IOException {
for (Session session : SessionSet) {
if(session.isOpen()){
SendMessage(session, message);
}
}
}
/**
* 指定Session发送消息
* @param sessionId
* @param message
* @throws IOException
*/
public static void SendMessage(String message,String sessionId) throws IOException {
Session session = null;
for (Session s : SessionSet) {
if(s.getId().equals(sessionId)){
session = s;
break;
}
}
if(session!=null){
SendMessage(session, message);
}
else{
log.warn("没有找到你指定ID的会话:{}",sessionId);
}
}
}
ServerEndpointExporter对象创建
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter () {
return new ServerEndpointExporter();
}
}
测试Controller
@RestController
@RequestMapping("/api/ws")
public class WebSocketController {
/**
* 群发消息内容
* @param message
* @return
*/
@RequestMapping(value="/sendAll", method= RequestMethod.GET)
public String sendAllMessage(@RequestParam(required=true) String message){
try {
WebSocketServer.BroadCastInfo(message);
} catch (IOException e) {
e.printStackTrace();
}
return "ok";
}
/**
* 指定会话ID发消息
* @param message 消息内容
* @param id 连接会话ID
* @return
*/
@RequestMapping(value="/sendOne", method=RequestMethod.GET)
public String sendOneMessage(@RequestParam(required=true) String message,@RequestParam(required=true) String id){
try {
WebSocketServer.SendMessage(message,id);
} catch (IOException e) {
e.printStackTrace();
}
return "ok";
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>websocket测试</title>
<style type="text/css">
h3,h4{
text-align:center;
}
</style>
</head>
<body>
<h3>WebSocket测试,在<span style="color:red">控制台</span>查看测试信息输出!</h3>
<h4>
[url=/api/ws/sendOne?message=单发消息内容&id=none]单发消息链接[/url]
[url=/api/ws/sendAll?message=群发消息内容]群发消息链接[/url]
</h4>
<script type="text/javascript">
var socket;
if (typeof (WebSocket) === undefined) {
console.error("遗憾:您的浏览器不支持WebSocket");
} else {
socket = new WebSocket("ws://localhost:7300/webSocket");
//连接打开事件
socket.onopen = function() {
console.log("Socket 已打开");
socket.send("消息发送测试(From Client)");
};
//收到消息事件
socket.onmessage = function(msg) {
console.log(msg.data);
};
//连接关闭事件
socket.onclose = function() {
console.log("Socket已关闭");
};
//发生了错误事件
socket.onerror = function() {
alert("Socket发生了错误");
};
//窗口关闭时,关闭连接
window.unload=function() {
socket.close();
};
}
</script>
</body>
</html>
2、websocket建立连接时传递参数
时间 2017-06-20
标签 websocket
原文 [http://blog.csdn.net/u014803081/article/details/73498233](javascript:void())
后台获取参数4种方法:
前端websocket的url
"ws:localhost:82/chat/123?username=ydh";
后端socket的注释
@ServerEndpoint("/chat/{param}")
socket的open方法
@OnOpen
public void open(Session session, @PathParam("param")String param) {
System.out.println("用户"+param+" 登录");
Map<String, String> map = session.getPathParameters();
System.out.println("session.getPathParameters()"+map.toString());
String str = session.getQueryString();
System.out.println("session.getQueryString()"+str);
String uri = session.getRequestURI().toString();
System.out.println("session.getRequestURI().toString()"+uri);
}
结果
用户123 登录
session.getPathParameters(){param=123}
session.getQueryString()username=baidu
session.getRequestURI().toString()/chat/123?username=baidu
1、url站位,通过@PathParam注释获取
2、url站位,通过session.getPathParameters()获取
3、url传参,通过session.getQueryString()
4、url传参,通过session.getRequestURI().toString()
从结果可以看出1、2两个方法不需要截取,可以直接使用;而3、4两个方法都需要截取字符串,稍微麻烦,但是请求的url比较清爽。
标签:webSocket,String,Session,通信协议,param,public,session,全双工,message From: https://www.cnblogs.com/tanmujin/p/17440811.html