【发布时间】:2016-07-01 16:34:30
【问题描述】:
我有一个来自 Official Netty 的 Echo 服务器示例 Echo Server
如何添加从 websocket 连接和流式传输的功能?
这是我的 ServerHandler 代码:
public class ServerHandler extends ChannelInboundHandlerAdapter
{
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
super.channelRegistered(ctx);
// !!!!! Think here should be WebSocket Handshake?
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
{
System.out.println(msg);
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx)
{
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
{
// Close the connection when an exception is raised.
cause.printStackTrace();
}
}
现在 Chrome 连接显示:WebSocket 连接到 'ws://127.0.0.1:8080/' 失败:WebSocket 握手期间出错:状态行无效
【问题讨论】: