handle method
Implementation
handle(DoxRequest req) async {
WebSocket websocket = await WebSocketTransformer.upgrade(req.httpRequest);
String socketId = 'websocket:${DateTime.now().microsecondsSinceEpoch}';
activeConnections[socketId] = {
"socket_id": socketId,
"websocket": websocket,
"previous_room": null,
"current_room": null,
};
websocket.listen(
(dynamic msg) async {
Map<String, dynamic> payload = jsonDecode(msg);
String event = payload['event'];
dynamic message = payload['message'];
if (events[event] != null) {
Function.apply(events[event] as Function,
[SocketEmitter(sender: socketId), message]);
}
if (event == 'joinRoom') {
_joinRoom(message, socketId);
}
},
onDone: () async {
_removeSocketIdFromCurrentRoom(socketId);
activeConnections.remove(socketId);
},
onError: (dynamic error) async {
_removeSocketIdFromCurrentRoom(socketId);
activeConnections.remove(socketId);
},
);
return websocket;
}