修复flutter analyze提示,更新版本至v1.0.0-Beta4.8
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
class ChatMessage {
|
||||
@@ -38,27 +39,46 @@ class ChatMessage {
|
||||
|
||||
class ChatService {
|
||||
WebSocketChannel? _channel;
|
||||
final StreamController<ChatMessage> _messageController = StreamController<ChatMessage>.broadcast();
|
||||
final StreamController<ChatMessage> _messageController =
|
||||
StreamController<ChatMessage>.broadcast();
|
||||
|
||||
Stream<ChatMessage> get messages => _messageController.stream;
|
||||
|
||||
void connect(String baseUrl, String roomId, String username) {
|
||||
final wsUri = Uri.parse(baseUrl).replace(scheme: 'ws', path: '/api/ws/room/$roomId', queryParameters: {'username': username});
|
||||
final wsUri = Uri.parse(baseUrl).replace(
|
||||
scheme: 'ws',
|
||||
path: '/api/ws/room/$roomId',
|
||||
queryParameters: {'username': username},
|
||||
);
|
||||
_channel = WebSocketChannel.connect(wsUri);
|
||||
|
||||
_channel!.stream.listen((data) {
|
||||
final json = jsonDecode(data);
|
||||
_messageController.add(ChatMessage.fromJson(json));
|
||||
}, onError: (err) {
|
||||
print("[WS ERROR] $err");
|
||||
}, onDone: () {
|
||||
print("[WS DONE] Connection closed");
|
||||
});
|
||||
_channel!.stream.listen(
|
||||
(data) {
|
||||
final json = jsonDecode(data);
|
||||
_messageController.add(ChatMessage.fromJson(json));
|
||||
},
|
||||
onError: (err) {
|
||||
debugPrint("[WS ERROR] $err");
|
||||
},
|
||||
onDone: () {
|
||||
debugPrint("[WS DONE] Connection closed");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void sendMessage(String content, String username, String roomId, {String type = 'chat'}) {
|
||||
void sendMessage(
|
||||
String content,
|
||||
String username,
|
||||
String roomId, {
|
||||
String type = 'chat',
|
||||
}) {
|
||||
if (_channel != null) {
|
||||
final msg = ChatMessage(type: type, username: username, content: content, roomId: roomId);
|
||||
final msg = ChatMessage(
|
||||
type: type,
|
||||
username: username,
|
||||
content: content,
|
||||
roomId: roomId,
|
||||
);
|
||||
_channel!.sink.add(jsonEncode(msg.toJson()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user