日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

c# websocket 心跳重连_websocket的简单使用

發(fā)布時間:2024/4/11 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# websocket 心跳重连_websocket的简单使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、maven項目,第一步需要配置jar包

org.springframework.boot

spring-boot-starter-websocket

2、前臺使用

var websocket = null;

//判斷當(dāng)前瀏覽器是否支持WebSocket

if('WebSocket' in window){

//連接WebSocket節(jié)點

websocket = new WebSocket("ws://localhost:81/api/imserver/"+用戶id;);

}else{

console.log("您的瀏覽器不支持WebSocket");

}

//連接發(fā)生錯誤的回調(diào)方法

websocket.onerror = function(){

};

//連接成功建立的回調(diào)方法

websocket.onopen = function(event){

}

//接收到消息的回調(diào)方法

websocket.onmessage = function(event){

setMessageInnerHTML(event.data);

}

//連接關(guān)閉的回調(diào)方法

websocket.onclose = function(){

}

//發(fā)送消息

function send(){

var message = document.getElementById('text').value;

websocket.send(message);

}

窗口關(guān)閉或者退出登錄時調(diào)用,如果掛掉需要重新請求

//監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。

window.onbeforeunload = function(){

websocket.close();

}

前臺檢測后臺是否掛掉

//針對斷網(wǎng)的情況的心跳重連

var heartCheck = {

timeout: 20000,//20ms

timeoutObj: null,

reset: function( ){

clearTimeout(this.timeoutObj);

  this.start();

},

start: function( ){

this.timeoutObj = setTimeout(function(){

websocket.send("HeartBeat"); //自定義需要執(zhí)行的命令

}, this.timeout)

}

};

heartCheck.start();

heartCheck.reset();

//將消息顯示在網(wǎng)頁上

function setMessageInnerHTML(innerHTML){

document.getElementById('message').innerHTML += innerHTML + '
';

}

3、后臺使用

新建WebSocketConfig 類

@Configuration

public class WebSocketConfig {

@Bean

public ServerEndpointExporter serverEndpointExporter() {

return new ServerEndpointExporter();

}

}

新建WebSocketServer類

@ServerEndpoint(value="/imserver/{userId}")

@Component

public class WebSocketServer {

private final static Logger log = LoggerFactory.getLogger(WebSocketServer.class);

/**靜態(tài)變量,用來記錄當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計成線程安全的。*/

private static int onlineCount = 0;

/**concurrent包的線程安全Set,用來存放每個客戶端對應(yīng)的MyWebSocket對象。*/

private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>();

/**與某個客戶端的連接會話,需要通過它來給客戶端發(fā)送數(shù)據(jù)*/

private Session session;

/**接收userId*/

private String userId="";

/**

* 連接建立成功調(diào)用的方法

**/

@OnOpen

public void onOpen(Session session,@PathParam("userId") String userId) {

this.session = session;

this.userId=userId;

if(webSocketMap.containsKey(userId)){

webSocketMap.remove(userId);

webSocketMap.put(userId,this);

//加入set中

}else{

webSocketMap.put(userId,this);

//加入set中

addOnlineCount();

//在線數(shù)加1

}

log.info("用戶連接:"+userId+",當(dāng)前在線人數(shù)為:" + getOnlineCount());

try {

sendMessage("");

} catch (IOException e) {

log.error("用戶:"+userId+",網(wǎng)絡(luò)異常!!!!!!");

}

}

/**

* 連接關(guān)閉調(diào)用的方法

*/

@OnClose

public void onClose() {

if(webSocketMap.containsKey(userId)){

webSocketMap.remove(userId);

//從set中刪除

subOnlineCount();

}

log.info("用戶退出:"+userId+",當(dāng)前在線人數(shù)為:" + getOnlineCount());

}

/**

* 收到客戶端消息后調(diào)用的方法

*

* @param message 客戶端發(fā)送過來的消息*/

@OnMessage

public void onMessage(String message, Session session) {

//log.info("用戶消息:"+userId+",報文:"+message);

//可以群發(fā)消息

//消息保存到數(shù)據(jù)庫、redis

if(StringUtils.isNotBlank(message)){

try {

if("ping".equals(message)) {

webSocketMap.get(userId).sendMessage("pong");

}else {

//解析發(fā)送的報文

JSONObject jsonObject = JSON.parseObject(message);

//追加發(fā)送人(防止串改)

jsonObject.put("fromUserId",this.userId);

String toUserId=jsonObject.getString("toUserId");

//傳送給對應(yīng)toUserId用戶的websocket

if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){

webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());

}else{

log.error("請求的userId:"+toUserId+"不在該服務(wù)器上");

//否則不在這個服務(wù)器上,發(fā)送到mysql或者redis

}

}

}catch (Exception e){

e.printStackTrace();

}

}

}

/**

* 用戶錯誤

* @param session

* @param error

*/

@OnError

public void onError(Session session, Throwable error) {

log.error("用戶錯誤:"+this.userId+",原因:"+error.getMessage());

}

/**

* 實現(xiàn)服務(wù)器主動推送

*/

public void sendMessage(String message) throws IOException {

this.session.getBasicRemote().sendText(message);

}

/**

* 發(fā)送自定義消息

* */

public static void sendInfo(String message,@PathParam("userId") String userId) {

try {

log.info("發(fā)送消息到:"+userId+",報文:"+message);

if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){

webSocketMap.get(userId).sendMessage(message);

}else{

log.error("用戶"+userId+",不在線!");

}

} catch (Exception e) {

log.error("異常"+userId+",報文:"+message);

}

}

public static synchronized int getOnlineCount() {

return onlineCount;

}

public static synchronized void addOnlineCount() {

WebSocketServer.onlineCount++;

}

public static synchronized void subOnlineCount() {

WebSocketServer.onlineCount--;

}

}

后臺使用時調(diào)用其發(fā)送方法,前臺就可以收到,然后修改頁面顯示

WebSocketServer.sendInfo("",userid);

謝謝觀看!!!

總結(jié)

以上是生活随笔為你收集整理的c# websocket 心跳重连_websocket的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。