SpringBoot-2.X 學習筆記08 整合WebSocket
1 在 SpringBoot 中加入依賴。
1 在 SpringBoot 的 pom.xml 中加入依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2 在 SpringBoot 增加一個配置。
package com.xu.springboot.websocket;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebSocketConfig.class);
}
@Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
return taskScheduler;
}
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3 在增加一個 WebSocket 消息發送工具類。
package com.xu.springboot.websocket;
import java.io.IOException;
import java.util.Map;
import javax.websocket.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class WebSocketUtils {
static Log log=LogFactory.getLog(WebSocketUtils.class);
public static void send_single_message(Session session,String message) {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
log.info(e);
}
}
public static void send_mutils_message(Map<String, Session> map,String msg) {
map.forEach((k,v)->{try {
v.getBasicRemote().sendText(msg);
} catch (IOException e) {
log.info(e);
}});
}
}
4 WebSocket 測試類。
4.1 Java類
package com.xu.springboot.websocket;
import java.io.IOException;
import java.util.Map;
import javax.websocket.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class WebSocketUtils {
static Log log=LogFactory.getLog(WebSocketUtils.class);
public static void send_single_message(Session session,String message) {
try {
session.getBasicRemote().sendText(message);
} catch (IOException e) {
log.info(e);
}
}
public static void send_mutils_message(Map<String, Session> map,String msg) {
map.forEach((k,v)->{try {
v.getBasicRemote().sendText(msg);
} catch (IOException e) {
log.info(e);
}});
}
}
4.2 HTML頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
自己:<input type="text" id="me" name="me" value="001" />
<br />
別人:<input type="text" id="username" name="username" value="002" />
<br />
信息:<input type="text" id="msg" name="msg" />
<br />
<input type="button" value="ReFresh" onclick="msg()" />
<br />
</body>
<script type="text/javascript">
function msg() {
var socket;
if (typeof (WebSocket) == "undefined") {
console.log("您的瀏覽器不支持WebSocket");
} else {
console.log("您的瀏覽器支持WebSocket");
//實現化WebSocket對象,指定要連接的服務器地址與端口 建立連接
index = new WebSocket("ws://localhost:8080/socket/{"+document.getElementById("me").value+"}/{"+document.getElementById("username").value+"}/{"+document.getElementById("msg").value+"}");
//socket = new WebSocket("${basePath}websocket/${cid}".replace("http","ws"));
//打開事件
index.onopen = function() {
console.log("Socket 已打開");
//socket.send("這是來自客戶端的消息" + location.href + new Date());
};
//獲得消息事件
index.onmessage = function(msg) {
console.log(msg.data);
document.getElementById("msg").value = msg.data;
};
//關閉事件
index.onclose = function() {
console.log("Socket已關閉");
};
//發生了錯誤事件
index.onerror = function() {
alert("Socket發生了錯誤");
}
}
}
</script>
</html>