Compare commits
2 Commits
cce33a5111
...
b86082c27d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b86082c27d | ||
|
|
0633a28407 |
@ -70,6 +70,12 @@
|
|||||||
<artifactId>pinyin4j</artifactId>
|
<artifactId>pinyin4j</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,19 @@
|
|||||||
|
package com.czlis.interfaceCommon.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket配置类,用于注册WebSocket的Bean
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class WebSocketConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServerEndpointExporter serverEndpointExporter() {
|
||||||
|
return new ServerEndpointExporter();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
package com.czlis.interfaceCommon.websocket;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.websocket.OnClose;
|
||||||
|
import javax.websocket.OnMessage;
|
||||||
|
import javax.websocket.OnOpen;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import javax.websocket.server.PathParam;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebSocket服务
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ServerEndpoint("/ws/{sid}")
|
||||||
|
public class WebSocketServer {
|
||||||
|
|
||||||
|
//存放会话对象
|
||||||
|
private static Map<String, Session> sessionMap = new HashMap();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接建立成功调用的方法
|
||||||
|
*/
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session session, @PathParam("sid") String sid) {
|
||||||
|
System.out.println("客户端:" + sid + "建立连接");
|
||||||
|
sessionMap.put(sid, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收到客户端消息后调用的方法
|
||||||
|
*
|
||||||
|
* @param message 客户端发送过来的消息
|
||||||
|
*/
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message, @PathParam("sid") String sid) {
|
||||||
|
System.out.println("收到来自客户端:" + sid + "的信息:" + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接关闭调用的方法
|
||||||
|
*
|
||||||
|
* @param sid
|
||||||
|
*/
|
||||||
|
@OnClose
|
||||||
|
public void onClose(@PathParam("sid") String sid) {
|
||||||
|
System.out.println("连接断开:" + sid);
|
||||||
|
sessionMap.remove(sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 群发
|
||||||
|
*
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
public void sendToAllClient(String message) {
|
||||||
|
Collection<Session> sessions = sessionMap.values();
|
||||||
|
for (Session session : sessions) {
|
||||||
|
try {
|
||||||
|
//服务器向客户端发送消息
|
||||||
|
session.getBasicRemote().sendText(message);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
6
pom.xml
6
pom.xml
@ -123,6 +123,12 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 阿里数据库连接池 -->
|
<!-- 阿里数据库连接池 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user