WebSocket 5 多人聊天系統

2021-09-16 12:32:46 字數 3196 閱讀 9652

功能說明:多人聊天系統,主要功能點:

2、實現群聊功能,我傳送訊息,大家都可以看到。

先看案例效果:

所以只能顯示最後顯示的使用者,如果放到redis就不會有這個問題。

@controller

public class userchatcontroller

/*** (這個實際開發中肯定存在redis或者session中,這樣資料才能共享)

* 這裡只是簡單的做個模擬,所以暫且用普通map吧

*/public static maponlineuser = new hashmap<>();

static

/***3、 功能描述:使用者登入介面

/***5、 功能描述 群聊天介面

* message 訊息體

* headeraccessor 訊息頭訪問器,通過這個獲取sessionid

*/public void topicchat(inmessage message, simpmessageheaderaccessor headeraccessor)

}

/**

* websocket握手請求的***. 檢查握手請求和響應, 對websockethandler傳遞屬性

* 可以通過這個類的方法獲取resuest,和response

}//在握手之後執行該方法. 無論是否握手成功都指明了響應狀態碼和相應頭(這個專案沒有用到該方法)

* 功能描述:頻道*** ,類似管道,可以獲取訊息的一些meta資料

*/public class socketchannelintecepter extends channelinterceptoradapter

/*** 在訊息被實際傳送到頻道之前呼叫

*/@override

public message> presend(message> message, messagechannel channel)

/*** 傳送訊息呼叫後立即呼叫

*/@override

public void postsend(message> message, messagechannel channel,

boolean sent)

}/**

* 連線成功

*/private void connect(string sessionid)

/*** 斷開連線

*/private void disconnect(string sessionid)

}

既然寫了兩個***,那麼肯定需要在配置資訊裡去配置它們。

@enablewebsocketmessagebroker

public class websocketconfig implements websocketmessagebrokerconfigurer

/*** 配置訊息**(中介)

* enable******broker 服務端推送給客戶端的路徑字首

*/@override

public void configuremessagebroker(messagebrokerregistry registry)

@override

public void configureclientinboundchannel(channelregistration registration)

@override

public void configureclientoutboundchannel(channelregistration registration)

}

登陸頁面和群聊頁面就不細聊,貼上**就好。

index.html

chat.html

var stompclient = null;

//一載入就會呼叫該方法

function connect() , function (frame) );

stompclient.subscribe('/topic/onlineuser', function (result) );

});}//斷開連線

function disconnect()

setconnected(false);

console.log("disconnected");

}//傳送聊天記錄

function sendcontent()

//顯示聊天記錄

function showcontent(body)

function showonlieuser(body)

$(function () );

$( "#disconnect" ).click(function() );

$( "#send" ).click(function() );

});

github原始碼

我只是偶爾安靜下來,對過去的種種思忖一番。那些曾經的舊時光裡即便有過天真愚鈍,也不值得譴責。畢竟,往後的日子,還很長。不斷鼓勵自己,

天一亮,又是嶄新的起點,又是未知的征程(上校1)

websocket實現單聊

server file ws from flask import flask,request,render template from geventwebsocket.handler import websockethandler from geventwebsocket.websocket imp...

php redis 處理websocket聊天記錄

ini set display errors on class chatclass 傳送訊息時儲存聊天記錄 這裡用的redis儲存是list資料型別 兩個人的聊天用乙個list儲存 from 訊息傳送者id to 訊息接受者id meassage 訊息內容 返回值,當前聊天的總聊天記錄數 publi...

多人暢聊系統

利用socket網路程式設計技術,實現客戶端和伺服器模型。利用多執行緒技術,通過訊號量控制線程的同步與互斥。伺服器端利用生產者與消費者模型對資料進行收發。利用開源jsoncpp和ncurses庫。模擬http協議自定義協議,進行註冊 登入 退出操作。註冊 登入 退出用的是tcp協議,而聊天用的是ud...