Skip to content

Commit 57248dc

Browse files
committed
断线重连
1 parent 7bfc592 commit 57248dc

File tree

4 files changed

+81
-40
lines changed

4 files changed

+81
-40
lines changed

.idea/workspace.xml

Lines changed: 39 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/danbai/ys/websocket/CinemaSocket.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class CinemaSocket {
3838
* 房间池
3939
*/
4040
public static ConcurrentHashMap<Integer, CinemaRoom> ROOM_POOL = new ConcurrentHashMap<>();
41+
public static ConcurrentHashMap<String, Integer> DELETE_P00L =new ConcurrentHashMap<>();
4142
/**
4243
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
4344
*/
@@ -52,12 +53,21 @@ public class CinemaSocket {
5253
public void onOpen(Session session, @PathParam("username") String username) {
5354
this.session = session;
5455
this.username=username;
56+
if(DELETE_P00L.containsKey(username)){
57+
this.roomId=DELETE_P00L.get(username);
58+
}
5559
//加入POOL中
5660
POOL.put(session.getId(),this);
61+
//断线重连加房间
62+
if(roomId!=0){
63+
CinemaRoom room= ROOM_POOL.get(roomId);
64+
if(room!=null){
65+
ROOM_POOL.get(roomId).getSockets().add(session.getId());
66+
}
67+
}
5768
//在线数加1
5869
log.info("有新连接加入!当前在线人数为" + POOL.size());
5970
CinemaSocketManagement.info(session.getId());
60-
6171
}
6272

6373
/**
@@ -68,6 +78,7 @@ public void onClose(Session session) {
6878
//从POOL中删除
6979
CinemaSocket cinemaSocket = POOL.get(session.getId());
7080
if(cinemaSocket!=null){
81+
DELETE_P00L.put(cinemaSocket.getUsername(),cinemaSocket.roomId);
7182
if(cinemaSocket.roomId!=0){
7283
CinemaSocketManagement.exitRoom(session.getId());
7384
}
@@ -101,6 +112,7 @@ public void onMessage(String message,Session session) {
101112
case "sendChat":CinemaSocketManagement.sendChat(id,jsonObject.getString("msg"));break;
102113
case "sendUrl":CinemaSocketManagement.sendUrl(id,jsonObject.getString("url"));break;
103114
case "sendTime":CinemaSocketManagement.sendTime(id,jsonObject.getDouble("time"));break;
115+
case "transfer":CinemaSocketManagement.transfer(id,jsonObject.getString("id"));break;
104116
default:log.info(message);
105117
}}
106118
} catch (NullPointerException e) {

src/main/java/com/danbai/ys/websocket/CinemaSocketManagement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,11 @@ public static void sendTime(String socketId,double time){
214214
SpringUtil.getBean(CinemaSocketAsync.class).sendRoomMsgPassAuthor(room.getId(),sendMsg.toJSONString());
215215
}
216216
}
217+
public static void transfer(String socketId,String transferId){
218+
CinemaSocket cinemaSocket = CinemaSocket.POOL.get(socketId);
219+
CinemaRoom room = CinemaSocket.ROOM_POOL.get(cinemaSocket.getRoomId());
220+
if(room.getAuthorId().equals(socketId)){
221+
CinemaSocket.ROOM_POOL.get(cinemaSocket.getRoomId()).setAuthorId(transferId);
222+
}
223+
}
217224
}

src/main/resources/templates/yiqikan/index.html

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,14 @@ <h3 id="fangjianInfo"></h3>
128128
<script src="https://cdn.jsdelivr.net/npm/cdnbye@latest"></script>
129129
<script src="https://cdn.jsdelivr.net/npm/p2p-dplayer@latest"></script>
130130
<script src="https://cdn.p00q.cn/ys/js/AgoraRTCSDK-3.0.1.js"></script>
131-
<script src="https://cdn.p00q.cn/ys/js/materialize.min.js"></script>
131+
<script src="https://cdn.p00q.cn/ys/js/reconnecting-websocket.min.js"></script>
132132
<script th:inline="javascript">
133133
/*<![CDATA[*/
134134
var username = /*[[${session.user.username}]]*/ null;
135135
/*]]>*/
136136
fangzhu = null;
137137
ysurl = "";
138-
id = null;
139-
138+
wsurl="wss://dbys.vip/wss/cinema/socket/"+username;
140139
//播放器
141140
const dp = new DPlayer({
142141
autoplay: true,
@@ -190,10 +189,9 @@ <h3 id="fangjianInfo"></h3>
190189
Notiflix.Loading.Init({
191190
clickToClose: true
192191
});
193-
194192
});
195193

196-
var ws = new WebSocket('wss://dbys.vip/wss/cinema/socket/' + username);
194+
var ws = new ReconnectingWebSocket(wsurl);
197195
//回调、事件函数区
198196
ws.onopen = function (evt) {
199197
console.log('连接到服务器');
@@ -217,8 +215,11 @@ <h3 id="fangjianInfo"></h3>
217215
case "sendTime": sendTime(json.time); break;
218216
}
219217
};
220-
ws.onclose = function (evt) {
218+
ws.onclose = function () {
221219
console.log('断开了连接');
220+
if(fangzhu!=null){
221+
console.log(123)
222+
}
222223
};
223224
//加入房间
224225
function joinbt(e) {
@@ -357,7 +358,16 @@ <h3 id="fangjianInfo"></h3>
357358
$("#roomOnline").html('(' + data.online + ')');
358359
$(".user").remove();
359360
data.users.forEach(user => {
360-
u = '<div class="user" uid="' + user.id + '">' + user.username + '</div>';
361+
if(username==data.author&user.username!=username){
362+
u='<div class="user" uid="' + user.id + '">' + user.username +'<button class="btn btn-success" type="button" onclick="transfer('+user.id+')">给他房主</button>'+'</div>';
363+
}else{
364+
if(user.username==data.author){
365+
u = '<div class="user" uid="' + user.id + '">' + user.username + '👑</div>';
366+
}else {
367+
u = '<div class="user" uid="' + user.id + '">' + user.username + '</div>';
368+
}
369+
370+
}
361371
$('#users').append(u);
362372
})
363373
}
@@ -373,14 +383,18 @@ <h3 id="fangjianInfo"></h3>
373383
}
374384
);
375385
}
386+
function transfer(id){
387+
ws.send(toJsonStr({type:"transfer",id:id}));
388+
}
376389
//退出房间
377390
$('.exitRoom').click(function(){
391+
fangzhu=null;
392+
ysurl=null;
378393
ws.send(toJsonStr({type:"exitRoom"}))
379394
$("#shouye").show();
380395
$("#fangjian").hide();
381396
publish(rtc);
382397
dp.pause();
383-
ysurl="";
384398
})
385399
//url
386400
function sendUrl(url) {

0 commit comments

Comments
 (0)