java中Websocket的使用方法例子
什么試WebSocket
WebSocket是一個連接,這個連接是客戶端(頁面)與服務(wù)端之間的連接,處理兩者間通訊;
好處:HTML5定義了WebSocket協(xié)議,能更好的節(jié)省服務(wù)器資源和帶寬,并且能夠更實時地進(jìn)行通訊,不需要每次發(fā)送請求接口獲取數(shù)據(jù),
使用場景:比如客戶端登錄后,出現(xiàn)的消息推送,每天定時廣播推送給客戶端消息等場景;
SpringBoot maven項目中實現(xiàn)
導(dǎo)入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
客戶端和服務(wù)端怎么鏈接呢?前端實現(xiàn)也是固定的寫法,只需要請求/websocket/{userId} 這個地址即可實現(xiàn)鏈接
前端js代碼:
//判斷當(dāng)前瀏覽器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket("ws://" + document.location.host + "/WebChat/websocket/" + username + "/"+ _img);
} else {
alert('當(dāng)前瀏覽器 Not support websocket')
}
java 實現(xiàn)websocket連接的代碼:
package org.jeecg.modules.message.websocket;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.WebsocketConst;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @Author wsf
* @Date 2023/04/29 9:41
* @Description: 此注解相當(dāng)于設(shè)置訪問URL
*/
@Component
@Slf4j
@ServerEndpoint("/websocket/{userId}") //此注解相當(dāng)于設(shè)置訪問URL
public class WebSocket {
private Session session;
private static final CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();
/*
websocket是客戶端和服務(wù)端之間建立了一個連接,建立完連接以后,會生成一個websocket對象,我們可以用這個對象來執(zhí)行發(fā)送,接收等操作。但是這只是一個存在于客戶端與服務(wù)器之間的鏈接,換句話說,系統(tǒng)只能識別到這個websocket連接是對應(yīng)于哪個頁面(瀏覽器),而這個頁面在系統(tǒng)中是對應(yīng)哪個用戶(數(shù)據(jù)庫中的用戶,或者根本就沒有對應(yīng)任何用戶,即未登錄,只是一個游客),我們是無法從這個websocket對象中獲取的。所以我們需要創(chuàng)建一個Map對象,用于將websocket對象和實際的user對象進(jìn)行關(guān)聯(lián),這樣為我們后續(xù)向特定的用戶推送消息做鋪墊
*/
private static final Map<String, Session> sessionPool = new HashMap<>();
@OnOpen
public void onOpen(Session session, @PathParam(value = "userId") String userId) {
try {
this.session = session;
webSockets.add(this);
sessionPool.put(userId, session);
log.info("【websocket消息】有新的連接,總數(shù)為: {}", webSockets.size());
} catch (Exception e) {
}
}
@OnClose
public void onClose(@PathParam(value = "userId") String userId) {
try {
webSockets.remove(this);
sessionPool.remove(userId);
log.info("【websocket消息】連接斷開,總數(shù)為: {}", webSockets.size());
} catch (Exception e) {
}
}
@OnMessage
public void onMessage(String message) {
log.debug("【websocket消息】收到客戶端消息: {}", message);
JSONObject obj = new JSONObject();
obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);//業(yè)務(wù)類型
obj.put(WebsocketConst.MSG_TXT, "心跳響應(yīng)");//消息內(nèi)容
session.getAsyncRemote().sendText(obj.toJSONString());
}
@OnError
public void OnError(Session session, @PathParam(value = "userId") String userId, Throwable t) {
try {
if (session.isOpen()) {
session.close();
}
webSockets.remove(this);
sessionPool.remove(userId);
log.info("【websocket消息】連接[錯誤]斷開,總數(shù)為: {}, 錯誤:{}", webSockets.size(), t.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
// 此為廣播消息
public void sendAllMessage(String message) {
log.info("【websocket消息】廣播消息:" + message);
for (WebSocket webSocket : webSockets) {
try {
if (webSocket.session.isOpen()) {
webSocket.session.getAsyncRemote().sendText(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 此為單點消息
public void sendOneMessage(String userId, String message) {
Session session = sessionPool.get(userId);
if (session != null && session.isOpen()) {
try {
log.info("【websocket消息】 單點消息:" + message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 此為單點消息(多人)
public void sendMoreMessage(String[] userIds, String message) {
for (String userId : userIds) {
Session session = sessionPool.get(userId);
if (session != null && session.isOpen()) {
try {
log.info("【websocket消息】 單點消息:" + message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}在RunApplication中加入:
package org.jeecg.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* 會自動注冊使用了@ServerEndpoint注解聲明的Websocket endpoint
* 要注意,如果使用獨(dú)立的servlet容器,
* 而不是直接使用springboot的內(nèi)置容器,
* 就不要注入ServerEndpointExporter,因為它將由容器自己提供和管理。
* 注入ServerEndpointExporter,
* 這個bean會自動注冊使用了@ServerEndpoint注解聲明的Websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}可以編寫一個test 測試連接
public class WebSocketTest {
public static void main(String[] args) {
try {
// 創(chuàng)建WebSocket客戶端
MyWebSocketClient myClient = new MyWebSocketClient(new URI("ws://127.0.0.1:9091/web/websocket/123333"));
// 與服務(wù)端建立連接
myClient.connect();
while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
System.out.println("連接中。。。");
Thread.sleep(1000);
}
// 往websocket服務(wù)端發(fā)送數(shù)據(jù)
myClient.send("發(fā)送來自websocketClient 123333的消息");
Thread.sleep(1000);
// 關(guān)閉與服務(wù)端的連接
// myClient.close();
}catch (Exception e){
e.printStackTrace();
}
// write your code here
}
}實際開發(fā)使用:
package org.jeecg.modules.food.job;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.modules.food.entity.DailyMenu;
import org.jeecg.modules.food.mapper.DailyMenuMapper;
import org.jeecg.modules.message.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @Auther: wsf
* @Date: 2023/5/30 15:52
* @Description:定時每天10、14點定時推送訂餐消息
*/
@Component
public class DailyMenuTask {
@Resource
private WebSocket webSocket;
@Autowired
private DailyMenuMapper dailyMenuMapper;
@Scheduled(cron = "0 0 10,14 * * ?")
public void pushDailyMenu() {
DailyMenu dailyMenu = new DailyMenu();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 1);
Date time = cal.getTime();
dailyMenu.setFoodDate(time);
List<DailyMenu> dailyMenus = dailyMenuMapper.selectByDate(dailyMenu);
if (CollectionUtils.isNotEmpty(dailyMenus)) {
//創(chuàng)建業(yè)務(wù)消息信息
JSONObject obj = new JSONObject();
// 業(yè)務(wù)類型
obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
obj.put(WebsocketConst.MSG_ID, dailyMenus.get(0).getId());
obj.put(WebsocketConst.MSG_TXT, "訂餐發(fā)布");
//全體發(fā)送
webSocket.sendAllMessage(obj.toJSONString());
}
}
}java 實現(xiàn)websocket的方式有很多種
- java 實現(xiàn)websocket一般兩種方式,一種使用tomcat的websocket實現(xiàn),比如上述使用這種方式無需別的任何配置,只需服務(wù)端一個處理類;
- 使用spring的websocket,spring與websocket整合需要spring 4.x,并且使用了socketjs,對不支持websocket的瀏覽器可以模擬websocket使用。實現(xiàn)WebSocketConfigurer接口的registerWebSocketHandlers方法加入自己WebSocketHandler接口的實現(xiàn)類。
- 使用spring stomp封裝的方法,實現(xiàn)更簡單,配置注入后通過@sendto就可向客戶端發(fā)消息。
- 等等
總結(jié)
到此這篇關(guān)于java中Websocket的使用方法的文章就介紹到這了,更多相關(guān)java Websocket使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實現(xiàn)使用Websocket發(fā)送消息詳細(xì)代碼舉例
- 如何在Java中使用WebSocket協(xié)議
- springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available)
- Java實現(xiàn)WebSocket四個步驟
- java基于websocket實現(xiàn)im聊天功能
- Java?spring?MVC環(huán)境中實現(xiàn)WebSocket的示例代碼
- Java中實現(xiàn)WebSocket方法詳解
- 教你如何使用Java實現(xiàn)WebSocket
- 一步步教你如何使用Java實現(xiàn)WebSocket
- Java應(yīng)用層協(xié)議WebSocket實現(xiàn)消息推送
- java?WebSocket?服務(wù)端實現(xiàn)代碼
- Java中使用WebSocket的幾種方式
相關(guān)文章
Springboot 整合 Dubbo/ZooKeeper 實現(xiàn) SOA 案例解析
這篇文章主要介紹了Springboot 整合 Dubbo/ZooKeeper 詳解 SOA 案例,需要的朋友可以參考下2017-11-11
SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置,我們可以通過自定義配置數(shù)據(jù)庫配置類來解決這個問題,方式有很多,不同的業(yè)務(wù)采用的方式也不同,下面我簡單的介紹我們項目的使用的方法2022-06-06
基于hibernate框架在eclipse下的配置方法(必看篇)
下面小編就為大家?guī)硪黄趆ibernate框架在eclipse下的配置方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

