Skip to content

Commit 49c15a6

Browse files
bug fix on websocket room id (#81)
1 parent 9145610 commit 49c15a6

File tree

7 files changed

+68
-65
lines changed

7 files changed

+68
-65
lines changed

packages/dox-websocket/lib/src/utils/constant.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ const String WEB_SOCKET_MESSAGE_KEY = 'message';
99
const String WEB_SOCKET_SENDER_KEY = 'sender';
1010

1111
const String WEB_SOCKET_ROOM_KEY = 'room';
12-
13-
const String WEB_SOCKET_DEFAULT_ROOM_NAME = '__default';

packages/dox-websocket/lib/src/websocket_emit_event.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ class WebsocketEmitEvent {
88
final String event;
99
final List<dynamic> exclude;
1010

11-
const WebsocketEmitEvent(
12-
this.senderId, this.roomId, this.message, this.event, this.exclude);
11+
const WebsocketEmitEvent({
12+
required this.senderId,
13+
required this.roomId,
14+
required this.message,
15+
required this.event,
16+
required this.exclude,
17+
});
1318

1419
@override
1520
toString() {
@@ -25,11 +30,11 @@ class WebsocketEmitEvent {
2530
static fromString(String json) {
2631
Map<String, dynamic> jsond = JSON.parse(json);
2732
return WebsocketEmitEvent(
28-
jsond['senderId'],
29-
jsond['roomId'],
30-
jsond['message'],
31-
jsond['event'],
32-
jsond['exclude'],
33+
senderId: jsond['senderId'],
34+
roomId: jsond['roomId'],
35+
message: jsond['message'],
36+
event: jsond['event'],
37+
exclude: jsond['exclude'],
3338
);
3439
}
3540

@@ -38,8 +43,7 @@ class WebsocketEmitEvent {
3843
WEB_SOCKET_EVENT_KEY: event,
3944
WEB_SOCKET_MESSAGE_KEY: message,
4045
WEB_SOCKET_SENDER_KEY: senderId,
41-
WEB_SOCKET_ROOM_KEY:
42-
roomId == WEB_SOCKET_DEFAULT_ROOM_NAME ? null : roomId,
46+
WEB_SOCKET_ROOM_KEY: roomId,
4347
});
4448
}
4549
}

packages/dox-websocket/lib/src/websocket_emitter.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class WebsocketEmitter {
2323
/// ```
2424
/// emitter.room('ABC');
2525
/// ```
26-
WebsocketEmitter room(dynamic id) {
27-
_roomId = id;
26+
WebsocketEmitter room(dynamic room) {
27+
_roomId = room;
2828
return this;
2929
}
3030

@@ -35,8 +35,13 @@ class WebsocketEmitter {
3535
void emitToSender(String event, dynamic message) {
3636
WebsocketInfo? info = _storage.getWebSocketInfo(sender);
3737
if (info != null) {
38-
WebsocketEmitEvent emitEvent =
39-
WebsocketEmitEvent(sender, _roomId, message, event, []);
38+
WebsocketEmitEvent emitEvent = WebsocketEmitEvent(
39+
senderId: sender,
40+
roomId: _roomId,
41+
message: message,
42+
event: event,
43+
exclude: [],
44+
);
4045

4146
/// we do not need to send via adaptor
4247
/// since sender will be always on the same isolate
@@ -45,22 +50,27 @@ class WebsocketEmitter {
4550
}
4651
}
4752

48-
/// set message except the sender
53+
/// sent message except the sender
4954
/// ```
5055
/// emitter.emitExceptSender('event', message);
5156
/// ```
5257
void emitExceptSender(String event, dynamic message) {
5358
emit(event, message, exclude: <String>[sender]);
5459
}
5560

56-
/// set message to everyone in the room
61+
/// sent message to everyone in the room
5762
/// ```
5863
/// emitter.emit('event', message);
5964
/// ```
6065
void emit(String event, dynamic message,
6166
{List<String> exclude = const <String>[]}) {
62-
WebsocketEmitEvent emitEvent =
63-
WebsocketEmitEvent(sender, _roomId, message, event, exclude);
67+
WebsocketEmitEvent emitEvent = WebsocketEmitEvent(
68+
senderId: sender,
69+
roomId: _roomId,
70+
message: message,
71+
event: event,
72+
exclude: exclude,
73+
);
6474

6575
WebsocketAdapterInterface? adapter = WebsocketServer().getAdapter();
6676

@@ -70,7 +80,6 @@ class WebsocketEmitter {
7080
emitEventToSockets(emitEvent);
7181
return;
7282
}
73-
7483
adapter.emit(emitEvent);
7584
}
7685

packages/dox-websocket/lib/src/websocket_event.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ class WebsocketEventHandler implements WebsocketEvent {
3838
/// socketId is just `uuid` generated by us
3939
String socketId = _createSocketId();
4040

41+
/// set default room id as route identifier
42+
String defaultRoomId = 'room:${req.getRouteIdentifier()}';
43+
4144
/// prepare emitter to pass to controller
42-
WebsocketEmitter emitter =
43-
WebsocketEmitter(socketId, WEB_SOCKET_DEFAULT_ROOM_NAME);
45+
WebsocketEmitter emitter = WebsocketEmitter(socketId, defaultRoomId);
4446

4547
/// add to active ws connection storage to reuse later
4648
_storage.addWebSocketInfo(socketId, ws);
4749

4850
/// add socket id to the room default room is the route eg. /ws
49-
_storage.addWebSocketIdToRoom(socketId, WEB_SOCKET_DEFAULT_ROOM_NAME);
51+
_storage.addWebSocketIdToRoom(socketId, defaultRoomId);
5052

5153
/// send connected event to client
5254
emitter.emitToSender('connected', <String, String>{'id': socketId});
@@ -63,7 +65,7 @@ class WebsocketEventHandler implements WebsocketEvent {
6365
/// if event name is WEB_SOCKET_JOIN_ROOM_EVENT_NAME,
6466
/// join to the room and message is room name
6567
if (eventName == WEB_SOCKET_JOIN_ROOM_EVENT_NAME) {
66-
String room = message;
68+
String room = defaultRoomId + message;
6769
_storage.addWebSocketIdToRoom(socketId, room);
6870
return;
6971
}

packages/dox-websocket/test/config/app_config.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import 'router.dart';
55

66
class ResponseHandler extends ResponseHandlerInterface {
77
@override
8-
void handle(DoxResponse res) {}
8+
DoxResponse handle(DoxResponse res) {
9+
return res;
10+
}
911
}
1012

1113
AppConfig appConfig = AppConfig(
@@ -16,13 +18,13 @@ AppConfig appConfig = AppConfig(
1618
serverPort: 3004,
1719

1820
/// total multi-thread isolate to run
19-
totalIsolate: 3,
21+
totalIsolate: 1,
2022

2123
// cors configuration
2224
cors: CORSConfig(
23-
allowOrigin: '*',
24-
allowMethods: '*',
25-
allowCredentials: true,
25+
origin: '*',
26+
methods: '*',
27+
credentials: true,
2628
),
2729

2830
/// response handler
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import 'package:dox_core/dox_core.dart';
22
import 'package:dox_websocket/dox_websocket.dart';
33

4+
class WsController {
5+
intro(WebsocketEmitter emitter, dynamic message) {
6+
emitter.emit('intro', message);
7+
}
8+
9+
json(WebsocketEmitter emitter, dynamic message) {
10+
emitter.emit('json', message);
11+
}
12+
}
13+
414
class WebsocketRouter implements Router {
515
@override
616
List get middleware => [];
@@ -10,14 +20,11 @@ class WebsocketRouter implements Router {
1020

1121
@override
1222
void register() {
13-
Route.websocket('ws', (WebsocketEvent event) {
14-
event.on('intro', (WebsocketEmitter emitter, dynamic message) {
15-
emitter.room('ws').emit('intro', message);
16-
});
23+
WsController wsController = WsController();
1724

18-
event.on('json', (WebsocketEmitter emitter, dynamic message) {
19-
emitter.emitExceptSender('json', message);
20-
});
25+
Route.websocket('ws', (WebsocketEvent event) {
26+
event.on('intro', wsController.intro);
27+
event.on('json', wsController.json);
2128
});
2229
}
2330
}

packages/dox-websocket/test/dox_websocket_test.dart

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ void main() {
4141
WebSocket socket =
4242
await WebSocket.connect('ws://localhost:${appConfig.serverPort}/ws');
4343

44+
socket.listen((dynamic message) {
45+
Map<String, dynamic> data = JSON.parse(message);
46+
if (data['event'] == 'intro') {
47+
expect(data['message'], 'hello');
48+
}
49+
if (data['event'] == 'json') {
50+
expect(data['message']['title'], 'hello');
51+
}
52+
});
53+
4454
String data = jsonEncode(<String, String>{
4555
'event': 'intro',
4656
'message': 'hello',
@@ -51,41 +61,12 @@ void main() {
5161
'message': <String, String>{'title': 'hello'}
5262
});
5363

54-
socket.listen((dynamic message) {
55-
Map<String, dynamic> data = JSON.parse(message);
56-
if (data['event'] == 'intro') {
57-
expect(data['message'], 'hello');
58-
}
59-
});
60-
6164
socket.add(data);
6265
socket.add(jsonData);
6366

64-
WebSocket socket2 =
65-
await WebSocket.connect('ws://localhost:${appConfig.serverPort}/ws');
66-
67-
String joinRoomData = jsonEncode(<String, String>{
68-
'event': 'joinRoom',
69-
'message': 'ws',
70-
});
71-
72-
socket2.add(joinRoomData);
73-
74-
socket2.listen((dynamic message) {
75-
Map<String, dynamic> data = JSON.parse(message);
76-
if (data['event'] == 'intro') {
77-
expect(data['message'], 'hello');
78-
}
79-
80-
if (data['event'] == 'json') {
81-
expect(data['message']['title'], 'hello');
82-
}
83-
});
84-
8567
await Future<void>.delayed(Duration(seconds: 2));
8668

8769
await socket.close();
88-
await socket2.close();
8970
});
9071
});
9172
}

0 commit comments

Comments
 (0)