在我的应用程序中,我在客户端专用页脚接收来自管理员的消息/通知,我想到使用Web套接字[多套接字],但是msg只发送回一个套接字,即消息被发送回发送它的同一个套接字,而不是发送到其他套接字,希望附件以简单的方式呈现问题。
注意:所有套接字都响应socket onOpen和onClose,错误在onMessage的响应中!
我的代码是:
server.dart:
import 'dart:io';
import 'package:intl/intl.dart'; // for DateFormat
class myClient {
WebSocket _socket;
myClient(WebSocket ws){
_socket = ws;
_socket.listen(messageHandler,
onError: errorHandler,
onDone: finishedHandler);
}
void write(String message){ _socket.add(message); }
void messageHandler(String msg){
print('a client sent this msg $msg');
distributeMessage(msg);
}
void errorHandler(error){
print('one socket got error: $error');
removeClient(this);
_socket.close();
}
void finishedHandler() {
print('one socket had been closed');
distributeMessage('one socket had been closed');
removeClient(this);
_socket.close();
}
}
List<myClient> clients = new List();
void main() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
print("HttpServer listening...");
server.listen((HttpRequest request) {
if (WebSocketTransformer.isUpgradeRequest(request)){
WebSocketTransformer.upgrade(request).then(handleWebSocket);
}
else {
print("Regular ${request.method} request for: ${request.uri.path}");
serveRequest(request);
}
});
});
}
void handleWebSocket(WebSocket socket){
print('Client connected!');
myClient client = new myClient(socket);
addClient(client);
}
void serveRequest(HttpRequest request){
request.response.statusCode = HttpStatus.FORBIDDEN;
request.response.reasonPhrase = "WebSocket connections only";
request.response.close();
}
void distributeMessage(String msg){
var now = new DateTime.now();
var timeStamp = new DateFormat("[dd-MMM @h:mma]");
String formatTime = timeStamp.format(now);
for (myClient c in clients)c.write('${timeStamp.format(now)}: $msg ');
}
void addClient(myClient c){
clients.add(c);
}
void removeClient(myClient c){
clients.remove(c);
}
client.dart文件是:
import 'dart:html';
void main() {
Element fonixFooter = querySelector('#fonix-footer');
String server = 'ws://localhost:8080/';
WebSocket ws = new WebSocket(server);
ws.onOpen.listen((Event e) => fonixFooter.text='Connected to server'); // this is fine
ws.onMessage.listen((MessageEvent e){ <= this is not functioning!!
print('msg is: ${e.data}');
fonixFooter.innerHtml=e.data;
});
ws.onClose.listen((Event e) => fonixFooter.text='Connection to server lost...'); // this is fine
}
admin.dart是:
import 'dart:html';
void main() {
TextInputElement input = querySelector('#input');
ParagraphElement output = querySelector('#output');
String server = 'ws://localhost:8080/';
WebSocket ws = new WebSocket(server);
ws.onOpen.listen((Event e) {
outputMessage(output, 'Connected to server');
});
ws.onMessage.listen((MessageEvent e){
outputMessage(output, e.data);
});
ws.onClose.listen((Event e) {
outputMessage(output, 'Connection to server lost...');
});
input.onChange.listen((Event e){
ws.send(input.value.trim());
input.value = "";
});
}
void outputMessage(Element e, String message){
print(message);
e.appendText(message);
e.appendHtml('<br/>');
e.scrollTop = e.scrollHeight;
}
基于@luizmineo的反馈他测试了服务器端,我注意到我的代码工作当且仅当每个套接字发送了一些数据,套接字没有发送数据,没有收到任何东西,但仍然不明白为什么!以及如何解决它!
任何想法,我犯了错误!!任何有关更好方法的建议都表示赞赏。
答案 0 :(得分:1)
我通过要求每个套接字在连接时向服务器发送内容来解决它,所以我使用以下代码:
在main.dart和client.dart中,将onOpen更改为:
ws.onOpen.listen((Event e) => ws.send('Connected to server'));
server.dart中的将消息处理程序更改为:
void messageHandler(String msg){
if (msg == 'Connected to server')write('Connected to server'); // send msg to the new socket only
else distributeMessage(msg); // send msg to all opened sockets
}
我相信这是一个解决方案,希望听到直接答案!