Skip to content

Commit

Permalink
feat: draw socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucisokiu committed Oct 16, 2024
1 parent 08cb7f0 commit f9f9b83
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/constants/api_enpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ class ApiEndpoints {
// Chats
static const String chats = 'chats';
static const String chatsConversations = 'chats/conversations';

static const String baseUrl = 'https://service-v2.waterbus.tech/busapi/v1/';
static const String wsUrl = 'https://sfu-v2.waterbus.tech';
}
7 changes: 7 additions & 0 deletions lib/constants/socket_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ class SocketEvent {
static const String sendMessageSSC = 'SEND_MESSAGE_SSC';
static const String updateMessageSSC = 'UPDATE_MESSAGE_SSC';
static const String deleteMessageSSC = 'DELETE_MESSAGE_SSC';

static const String startWhiteBoardSSC = 'START_WHITE_BOARD_SSC';
static const String startWhiteBoardCSS = 'START_WHITE_BOARD_CSS';
static const String updateWhiteBoardCSS = 'UPDATE_WHITE_BOARD_CSS';
static const String updateWhiteBoardSSC = 'UPDATE_WHITE_BOARD_SSC';
static const String cleanWhiteBoardCSS = 'CLEAN_WHITE_BOARD_CSS';
static const String cleanWhiteBoardSSC = 'CLEAN_WHITE_BOARD_SSC';
}
4 changes: 4 additions & 0 deletions lib/core/websocket/interfaces/socket_emiter_interface.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/models/draw_model.dart';

abstract class SocketEmiter {
void establishBroadcast({
Expand Down Expand Up @@ -29,6 +30,9 @@ abstract class SocketEmiter {
void sendNewSdp(String sdp);
void leaveRoom(String roomId);
void setSubtitle(bool isEnabled);
void startWhiteBoard(int roomId);
void updateWhiteBoard(int roomId, DrawModel draw, String action);
void cleanWhiteBoard(int roomId);

void reconnect();
}
22 changes: 22 additions & 0 deletions lib/core/websocket/socket_emiter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:waterbus_sdk/core/websocket/interfaces/socket_emiter_interface.d
import 'package:waterbus_sdk/core/websocket/interfaces/socket_handler_interface.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/injection/injection_container.dart';
import 'package:waterbus_sdk/types/models/draw_model.dart';

@Injectable(as: SocketEmiter)
class SocketEmiterImpl extends SocketEmiter {
Expand Down Expand Up @@ -110,6 +111,27 @@ class SocketEmiterImpl extends SocketEmiter {
_socket?.emit(SocketEvent.setSubscribeSubtitleCSS, {'enabled': isEnabled});
}

@override
void startWhiteBoard(int roomId) {
_socket
?.emit(SocketEvent.startWhiteBoardCSS, {'roomId': roomId.toString()});
}

@override
void updateWhiteBoard(int roomId, DrawModel draw, String action) {
_socket?.emit(SocketEvent.updateWhiteBoardCSS, {
'roomId': roomId,
'action': action,
'paints': [draw.toMap()],
});
}

@override
void cleanWhiteBoard(int roomId) {
_socket
?.emit(SocketEvent.cleanWhiteBoardCSS, {'roomId': roomId.toString()});
}

@override
void reconnect() {
_socket?.emit(SocketEvent.reconnect);
Expand Down
38 changes: 38 additions & 0 deletions lib/core/websocket/socket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:waterbus_sdk/core/api/base/dio_configuration.dart';
import 'package:waterbus_sdk/core/webrtc/webrtc_interface.dart';
import 'package:waterbus_sdk/core/websocket/interfaces/socket_handler_interface.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/models/draw_model.dart';
import 'package:waterbus_sdk/types/models/draw_socket_event.dart';
import 'package:waterbus_sdk/utils/encrypt/encrypt.dart';
import 'package:waterbus_sdk/utils/extensions/duration_extensions.dart';
import 'package:waterbus_sdk/utils/logger/logger.dart';
Expand Down Expand Up @@ -320,6 +322,42 @@ class SocketHandlerImpl extends SocketHandler {
MessageSocketEvent(event: MessageEventEnum.delete, message: message),
);
});

_socket?.on(SocketEvent.startWhiteBoardSSC, (data) {
if (data == null) return;

final List<dynamic> drawDataSocket = data['paints'];
final List<DrawModel> drawList =
drawDataSocket.map((data) => DrawModel.fromMap(data)).toList();
WaterbusSdk.onDrawChanged?.call(
DrawSocketEvent(
event: DrawSocketEnum.start,
draw: drawList,
),
);
});
_socket?.on(SocketEvent.updateWhiteBoardSSC, (data) {
if (data == null) return;
final UpdateDrawEnum action = UpdateDrawEnum.fromString(data['action']);
final List drawDataSocket = data['paints'];
final List<DrawModel> drawList =
drawDataSocket.map((data) => DrawModel.fromMap(data)).toList();
WaterbusSdk.onDrawChanged?.call(
DrawSocketEvent(
event: DrawSocketEnum.update,
action: action,
draw: drawList,
),
);
});
_socket?.on(SocketEvent.cleanWhiteBoardSSC, (data) {
WaterbusSdk.onDrawChanged?.call(
DrawSocketEvent(
event: DrawSocketEnum.delete,
draw: [],
),
);
});
});
}

Expand Down
19 changes: 18 additions & 1 deletion lib/flutter_waterbus_sdk.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
library waterbus_sdk;

import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter_webrtc_plus/flutter_webrtc_plus.dart';

import 'package:waterbus_sdk/core/api/base/base_local_storage.dart';
import 'package:waterbus_sdk/core/webrtc/webrtc_interface.dart';
import 'package:waterbus_sdk/injection/injection_container.dart';
import 'package:waterbus_sdk/types/index.dart';
import 'package:waterbus_sdk/types/models/draw_model.dart';
import 'package:waterbus_sdk/types/models/draw_socket_event.dart';
import 'package:waterbus_sdk/utils/callkit/callkit_listener.dart';
import 'package:waterbus_sdk/waterbus_sdk_interface.dart';

Expand All @@ -24,6 +26,7 @@ class WaterbusSdk {
static Function(VideoSenderStats)? onStatsChanged;
static Function(Subtitle)? onSubtitle;
static Function(MessageSocketEvent)? onMesssageChanged;
static Function(DrawSocketEvent)? onDrawChanged;

set onMessageSocketChanged(Function(MessageSocketEvent) onMesssageChanged) {
WaterbusSdk.onMesssageChanged = onMesssageChanged;
Expand All @@ -41,6 +44,10 @@ class WaterbusSdk {
WaterbusSdk.onSubtitle = onSubtitle;
}

set onDrawSocketChanged(Function(DrawSocketEvent) onDrawChanged) {
WaterbusSdk.onDrawChanged = onDrawChanged;
}

Future<void> initializeApp({
required String wsUrl,
required String apiUrl,
Expand Down Expand Up @@ -111,6 +118,16 @@ class WaterbusSdk {
await _sdk.leaveRoom();
}

Future<void> getWhiteBoard(int roomId) async {
await _sdk.getWhiteBoard(roomId);
}

Future<void> updateWhiteBoard(int roomId,DrawModel draw, String action) async {
await _sdk.updateWhiteBoard(roomId,draw,action);
}
Future<void> cleanWhiteBoard(int roomId) async {
await _sdk.cleanWhiteBoard(roomId);
}
// Related to local media
Future<void> reconnect() async => await _sdk.reconnect();

Expand Down
46 changes: 46 additions & 0 deletions lib/types/enums/draw_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

enum DrawTypes {
normal,
eraser,
line,
polygon,
square,
circle;

static DrawTypes fromString(String value) {
switch (value) {
case 'normal':
return DrawTypes.normal;
case 'eraser':
return DrawTypes.eraser;
case 'line':
return DrawTypes.line;
case 'polygon':
return DrawTypes.polygon;
case 'square':
return DrawTypes.square;
case 'circle':
return DrawTypes.circle;
default:
return DrawTypes.normal;
}
}

@override
String toString() {
switch (this) {
case DrawTypes.normal:
return 'normal';
case DrawTypes.eraser:
return 'eraser';
case DrawTypes.line:
return 'line';
case DrawTypes.polygon:
return 'polygon';
case DrawTypes.square:
return 'square';
case DrawTypes.circle:
return 'circle';
}
}
}
Loading

0 comments on commit f9f9b83

Please sign in to comment.