Upgrade a regular HTTP request to a WebSocket request using WebSocketTransformer.upgrade() .
import 'dart:io';
handleMsg(msg) {
print('Message received: $msg');
}
main() async {
try {
var server = await HttpServer.bind('127.0.0.1', 4040);
await for (HttpRequest req in server) {
if (req.uri.path == '/ws') {
// Upgrade an HttpRequest to a WebSocket connection.
var socket = await WebSocketTransformer.upgrade(req);
socket.listen(handleMsg);
}
}
} catch (e) {
print(e);
}
}
查看更多关于快速构建WebSocket服务端的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did129447