问题描述
我正在尝试实现一个基本的服务器,它同时为 websockets 和 http 请求提供服务。
代码是这样的;
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_web_socket/shelf_web_socket.dart';
void main(List<String> args) async {
var app = Router();
var wsHandle = webSocketHandler((webSocket) {
webSocket.stream.listen((message) {
print(message);
webSocket.sink.add("echo $message");
});
});
app.get('/',(Request r) {
return Response.ok('hello-world');
});
app.get("/ws",wsHandle);
var server = await io.serve(app,'localhost',8080);
print("Server is on at ${server.address.host} ${server.port}");
}
当我尝试连接 ws url 时,我收到劫持错误。
Exception has occurred.
HijackException (A shelf request's underlying data stream was hijacked.
This exception is used for control flow and should only be handled by a Shelf adapter.)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)