公司动态
简单的Websocket程序示例(Spring Boot)
该示例的工程代码我已经上传到CSDN资源网站下载地址是https://download.csdn.net/download/look4liming/109575041、首先建一个空的Spring Boot项目2、在pom.xml中添加如下依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-websocket/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-tomcat/artifactId scopeprovided/scope /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency3、新增一个Configuration类注意这个类必须有否则Websocket服务类的注解配置无法生效。import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * 必须有此类否则Websocket配置不生效原因尚未查清。 * author Bright Lee */ Configuration public class WebSocketConfig { Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }4、新增Websocket服务类import java.io.IOException; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.springframework.stereotype.Component; ServerEndpoint(value /websocket) Component public class WebSocketServer { private Session session; OnOpen public void onOpen(Session session) { this.session session; try { sendMessage(连接成功); } catch (IOException e) { e.printStackTrace(); } } OnClose public void onClose() { } OnMessage public void onMessage(String message, Session session) { try { sendMessage(您发送的消息是--- message); } catch (IOException e) { e.printStackTrace(); } } OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } public void sendMessage(String message) throws IOException { session.getBasicRemote().sendText(message); } }5、新增index.html!DOCTYPE html html head meta charsetUTF-8 titleWebsocket客户端/title script var websocket null; var content null; function init() { content document.getElementById(content); } function printToScreen(message) { var pElement document.createElement(p); pElement.innerHTML message; content.appendChild(pElement); } function connect() { if (websocket ! null) { return; } var url ws://127.0.0.1:8080/websocket; printToScreen(开始连接 url); websocket new WebSocket(url); websocket.onopen function(event) { printToScreen(连接成功); } websocket.onmessage function(event) { printToScreen(收到消息 event.data); } websocket.onerror function(event) { printToScreen(出现错误 event.data); closeWebsocket(); } } function sendMessage() { if (websocket null) { return; } websocket.send(messageInput.value); printToScreen(发送消息 messageInput.value); } function closeWebsocket() { if (websocket null) { return; } websocket.close(); websocket null; printToScreen(已关闭); } /script /head body onloadinit(); div styletext-align: left; form action input typetext idmessageInput value你好 input typebutton onclickconnect(); value连接 input typebutton onclicksendMessage(); value发送 input typebutton onclickcloseWebsocket(); value关闭 br / /form /div div idcontent/div /body /html6、启动Spring Boot项目访问index.html在浏览器中输入http://127.0.0.1:8080/index.html青蛙客服系统https://download.csdn.net/download/look4liming/93326820