Springboot基于websocket實(shí)現(xiàn)簡單在線聊天功能
添加maven依賴
<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-websocket</artifactId> <name>springboot-websocket</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> </dependencies></project>
添加websocket配置
@Configuration@EnableWebSocketpublic class MyWebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), '/myHandler').setAllowedOrigins('*'); } @Bean public WebSocketHandler myHandler() { return new MyTextWebSocketHandler(); }}
實(shí)現(xiàn)具體的handler
public class MyTextWebSocketHandler extends TextWebSocketHandler { private Set<WebSocketSession> sessions = new HashSet<>(); @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { if (session.isOpen()) { sessions.add(session); } sendToAll(message); } private void sendToAll(TextMessage message) throws IOException { for (WebSocketSession session : sessions) { session.sendMessage(message); } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { sessions.remove(session); }}
即可通過ws://localhost:8080/myHandler訪問websocket
添加測試頁面:
<html><script type='text/javascript'> if ('WebSocket' in window) { var ws = new WebSocket('ws://localhost:8080/myHandler'); ws.onopen = function () { }; ws.onmessage = function (evt) { document.getElementById(’messageDiv’).innerHTML += evt.data + '</br>'; }; ws.onclose = function () { console.log('close connect'); }; } else { alert('您的瀏覽器不支持 WebSocket!'); } function send() { ws.send(document.getElementById('input').value + ': ' + document.getElementById('message').value); }</script></head><body>當(dāng)前用戶: <input /><br/><a href='http://www.baoyu77737.com/bcjs/13700.html#' rel='external nofollow' onclick='send();'>發(fā)送消息</a>: <input /><div id='messageDiv'></div></body></html>
即可實(shí)現(xiàn)簡單的通信功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解JSP 內(nèi)置對象request常見用法2. ASP.NET MVC實(shí)現(xiàn)下拉框多選3. ASP.NET MVC增加一條記錄同時添加N條集合屬性所對應(yīng)的個體4. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別5. 解決request.getParameter取值后的if判斷為NULL的問題6. JSP中param動作的實(shí)例詳解7. ASP.NET MVC實(shí)現(xiàn)本地化和全球化8. .Net反向代理組件Yarp用法詳解9. JS中的常見數(shù)組遍歷案例詳解(forEach, map, filter, sort, reduce, every)10. .NET中的MassTransit分布式應(yīng)用框架詳解
