Skip to content

Commit 7f4c0fd

Browse files
committed
optimize network
1 parent 3a47efc commit 7f4c0fd

File tree

8 files changed

+27
-58
lines changed

8 files changed

+27
-58
lines changed

CarzyCarServer/src/main/java/com/tastsong/crazycar/controller/MatchRoomWebSocket.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.tastsong.crazycar.controller;
22

33
import javax.websocket.*;
4-
import javax.websocket.server.PathParam;
54
import javax.websocket.server.ServerEndpoint;
65

76
import org.springframework.context.ApplicationContext;
@@ -25,7 +24,7 @@
2524

2625

2726

28-
@ServerEndpoint("/websocket/MatchRoomWebSocket/{id}")
27+
@ServerEndpoint("/websocket/MatchRoomWebSocket")
2928
@Data
3029
@Slf4j
3130
@NoArgsConstructor
@@ -56,17 +55,13 @@ public class MatchRoomPlayerInfo{
5655

5756
// id = uid + "," + room_id
5857
@OnOpen
59-
public void onOpen(@PathParam(value = "id") String param, Session WebSocketsession, EndpointConfig config) {
60-
id = param;//接收到发送消息的人员编号
61-
curUid = Integer.parseInt(id.split(",")[0]);
62-
roomId = id.split(",")[1];
58+
public void onOpen(Session WebSocketsession, EndpointConfig config) {
6359
this.WebSocketsession = WebSocketsession;
64-
webSocketSet.put(param, this);
6560
addOnlineCount();
6661

6762
ApplicationContext act = ApplicationContextRegister.getApplicationContext();
6863
matchService = act.getBean(MatchService.class);
69-
log.info("Match Room onOpen, num = " + getOnlineCount() + " uid = " + curUid + " roomId = " + roomId);
64+
log.info("Match Room onOpen, num = " + getOnlineCount());
7065
}
7166

7267

@@ -110,21 +105,24 @@ public void onMessage(String message, Session session) {
110105
}
111106

112107
private void onCreateRoom(JSONObject message) {
113-
Integer uid = message.getInt("uid");
114-
String roomId = message.getStr("room_id");
108+
curUid = message.getInt("uid");
109+
roomId = message.getStr("room_id");
110+
id = curUid + "," + roomId;
111+
webSocketSet.put(id, this);
112+
System.out.println("++++++ " + id);
115113
String token = message.getStr("token");
116114
JSONObject data = new JSONObject();
117115
data.putOpt("msg_type", Util.msgType.MatchRoomCreate);
118-
data.putOpt("uid", uid);
116+
data.putOpt("uid", curUid);
119117
if(!Util.isLegalToken(token)){
120118
data.putOpt("code", 423);
121119
} else if (MatchRoomWebSocket.roomMap.containsKey(roomId)){
122120
data.putOpt("code", 421);
123121
} else{
124122
MatchRoomPlayerInfo info = new MatchRoomPlayerInfo();
125-
info.uid = uid;
126-
info.memberName = matchService.getUserName(uid);
127-
info.aid = matchService.getAid(uid);
123+
info.uid = curUid;
124+
info.memberName = matchService.getUserName(curUid);
125+
info.aid = matchService.getAid(curUid);
128126
info.canWade = matchService.canWade(message.getInt("eid"));
129127
info.isHouseOwner = true;
130128
ArrayList<MatchRoomPlayerInfo> list = new ArrayList<MatchRoomPlayerInfo>();
@@ -137,8 +135,10 @@ private void onCreateRoom(JSONObject message) {
137135
}
138136

139137
private void onJoinRoom(JSONObject message) {
140-
Integer uid = message.getInt("uid");
141-
String roomId = message.getStr("room_id");
138+
curUid = message.getInt("uid");
139+
roomId = message.getStr("room_id");
140+
id = curUid + "," + roomId;
141+
webSocketSet.put(id, this);
142142
String token = message.getStr("token");
143143
JSONObject data = new JSONObject();
144144
data.putOpt("msg_type", Util.msgType.MatchRoomJoin);
@@ -150,9 +150,9 @@ private void onJoinRoom(JSONObject message) {
150150
data.putOpt("code", 423);
151151
} else{
152152
MatchRoomPlayerInfo info = new MatchRoomPlayerInfo();
153-
info.uid = uid;
154-
info.memberName = matchService.getUserName(uid);
155-
info.aid = matchService.getAid(uid);
153+
info.uid = curUid;
154+
info.memberName = matchService.getUserName(curUid);
155+
info.aid = matchService.getAid(curUid);
156156
info.canWade = matchService.canWade(message.getInt("eid"));
157157
info.isHouseOwner = false;
158158
MatchRoomWebSocket.roomMap.get(roomId).add(info);

CarzyCarServer/src/main/java/com/tastsong/crazycar/controller/MatchWebSocket.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,13 @@ public class MatchWebSocket {
3333
//当前发消息的人员编号
3434
private String id = "";
3535

36-
/**
37-
* 连接建立成功调用的方法
38-
*
39-
* @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
40-
*/
4136
@OnOpen
4237
public void onOpen(Session WebSocketsession, EndpointConfig config) {
4338
this.WebSocketsession = WebSocketsession;
4439
addOnlineCount(); //在线数加1
4540
log.info("Match onOpen num = " + getOnlineCount());
4641
}
4742

48-
/**
49-
* 连接关闭调用的方法
50-
*/
5143
@OnClose
5244
public void onClose() {
5345
if (!id.equals("")) {
@@ -58,12 +50,6 @@ public void onClose() {
5850
}
5951
}
6052

61-
/**
62-
* 收到客户端消息后调用的方法
63-
*
64-
* @param message 客户端发送过来的消息
65-
* @param session 可选的参数
66-
*/
6753
@OnMessage
6854
public void onMessage(String message, Session session) {
6955
JSONObject sendMsg = JSONUtil.parseObj(message);
@@ -74,10 +60,7 @@ public void onMessage(String message, Session session) {
7460
sendToUser(sendMsg);
7561
}
7662
}
77-
/**
78-
* 给指定的人发送消息
79-
* @param message
80-
*/
63+
8164
private void sendToUser(JSONObject message) {
8265
String cid = message.getStr("cid");
8366
Integer uid = message.getInt("uid");
@@ -108,26 +91,14 @@ private void onCreatePlayer(JSONObject data){
10891
}
10992
}
11093

111-
/**
112-
* 发生错误时调用
113-
*
114-
* @param session
115-
* @param error
116-
*/
11794
@OnError
11895
public void onError(Session session, Throwable error) {
11996
log.info("Match Websocket onError");
12097
error.printStackTrace();
12198
}
122-
/**
123-
* 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
124-
*
125-
* @param message
126-
* @throws IOException
127-
*/
99+
128100
private void sendMessage(JSONObject message) throws IOException {
129101
this.WebSocketsession.getBasicRemote().sendText(message.toString());
130-
//this.session.getAsyncRemote().sendText(message);
131102
}
132103

133104
public static synchronized int getOnlineCount() {
Binary file not shown.

CrazyCar/Assets/Scenes/Login.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.44657815, g: 0.49641192, b: 0.57481617, a: 1}
41+
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
@@ -2073,7 +2073,7 @@ MonoBehaviour:
20732073
m_Script: {fileID: 11500000, guid: da1b902846b36a245810a5b71322bcba, type: 3}
20742074
m_Name:
20752075
m_EditorClassIdentifier:
2076-
serverType: 2
2076+
serverType: 0
20772077
netType: 0
20782078
--- !u!1 &1019334752
20792079
GameObject:

CrazyCar/Assets/Scripts/System/MatchRoomSystem.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public interface IMatchRoomSystem : ISystem {
2323
public class MatchRoomSystem : AbstractSystem, IMatchRoomSystem {
2424
private void MatchRoomConnect(Action succ) {
2525
string ws = "ws" + this.GetSystem<INetworkSystem>().HttpBaseUrl.Substring(4) +
26-
RequestUrl.matchRoomWSUrl +
27-
this.GetModel<IUserModel>().Uid.Value + "," + this.GetModel<IMatchModel>().RoomId;
28-
26+
RequestUrl.matchRoomWSUrl;
2927
this.GetSystem<IWebSocketSystem>().Connect(ws);
3028
this.GetSystem<IWebSocketSystem>().ConnectSuccAction = succ;
3129
}

CrazyCar/Assets/Scripts/Utility/PathRecorderUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static class RequestUrl {
2626
public static string enterRoomUrl = "v2/Game/EnterGame";
2727
public static string getUserInfo = "v2/UserInfo/GetUser";
2828

29-
public static string matchRoomWSUrl = "websocket/MatchRoomWebSocket/";
29+
public static string matchRoomWSUrl = "websocket/MatchRoomWebSocket";
3030
public static string matchWSUrl = "websocket/MatchWebSocket";
3131
public static string timeTrialWSUrl = "websocket/TimeTrialWebSocket";
3232

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2452c32702ddf771abb45599d3a4f6d6
1+
425e08867a07a42cb646496c8352063d

CrazyCar/Assets/StreamingAssets/StandaloneWindows/catalog_CrazyCar.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)