c# websocket 心跳重连_websocket的简单使用
1、maven項(xiàng)目,第一步需要配置jar包
org.springframework.boot
spring-boot-starter-websocket
2、前臺(tái)使用
var websocket = null;
//判斷當(dāng)前瀏覽器是否支持WebSocket
if('WebSocket' in window){
//連接WebSocket節(jié)點(diǎn)
websocket = new WebSocket("ws://localhost:81/api/imserver/"+用戶id;);
}else{
console.log("您的瀏覽器不支持WebSocket");
}
//連接發(fā)生錯(cuò)誤的回調(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)閉或者退出登錄時(shí)調(diào)用,如果掛掉需要重新請(qǐng)求
//監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時(shí),主動(dòng)去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會(huì)拋異常。
window.onbeforeunload = function(){
websocket.close();
}
前臺(tái)檢測(cè)后臺(tái)是否掛掉
//針對(duì)斷網(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)頁(yè)上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '
';
}
3、后臺(tái)使用
新建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)變量,用來(lái)記錄當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計(jì)成線程安全的。*/
private static int onlineCount = 0;
/**concurrent包的線程安全Set,用來(lái)存放每個(gè)客戶端對(duì)應(yīng)的MyWebSocket對(duì)象。*/
private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>();
/**與某個(gè)客戶端的連接會(huì)話,需要通過(guò)它來(lái)給客戶端發(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ā)送過(guò)來(lái)的消息*/
@OnMessage
public void onMessage(String message, Session session) {
//log.info("用戶消息:"+userId+",報(bào)文:"+message);
//可以群發(fā)消息
//消息保存到數(shù)據(jù)庫(kù)、redis
if(StringUtils.isNotBlank(message)){
try {
if("ping".equals(message)) {
webSocketMap.get(userId).sendMessage("pong");
}else {
//解析發(fā)送的報(bào)文
JSONObject jsonObject = JSON.parseObject(message);
//追加發(fā)送人(防止串改)
jsonObject.put("fromUserId",this.userId);
String toUserId=jsonObject.getString("toUserId");
//傳送給對(duì)應(yīng)toUserId用戶的websocket
if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){
webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
}else{
log.error("請(qǐng)求的userId:"+toUserId+"不在該服務(wù)器上");
//否則不在這個(gè)服務(wù)器上,發(fā)送到mysql或者redis
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 用戶錯(cuò)誤
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用戶錯(cuò)誤:"+this.userId+",原因:"+error.getMessage());
}
/**
* 實(shí)現(xiàn)服務(wù)器主動(dòng)推送
*/
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+",報(bào)文:"+message);
if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){
webSocketMap.get(userId).sendMessage(message);
}else{
log.error("用戶"+userId+",不在線!");
}
} catch (Exception e) {
log.error("異常"+userId+",報(bào)文:"+message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
后臺(tái)使用時(shí)調(diào)用其發(fā)送方法,前臺(tái)就可以收到,然后修改頁(yè)面顯示
WebSocketServer.sendInfo("",userid);
謝謝觀看!!!
總結(jié)
以上是生活随笔為你收集整理的c# websocket 心跳重连_websocket的简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jedis 读写分离_redis读写分离
- 下一篇: 启动sqlserver_微软的 SQL