私はnettyフレームワークバージョン4.1.6でwebsocketサーバを開発しています。私はnetty example site からのサンプルコードを使用しています これは私のサーバーのソースコードです:javascriptクライアントとnettyサーバの間の安全なwebsocket接続
public class WebSocketServer
{
static final int PORT = 4466;
public static void main(String[] args)
{
final SslContext sslCtx;
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new WebSocketServerInitializer(sslCtx));
Channel ch = b.bind(PORT).sync().channel();
System.out.println("Open your web browser and navigate to " +
"http://127.0.0.1:" + PORT + '/');
ch.closeFuture().sync();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
アンWebSocketServerInitializerソースコード:
<html>
<head>
<script type="text/javascript">
// Let us open a web socket
var ws = null;
function WebSocketTest()
{
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
ws = new WebSocket("wss://localhost:4466/websocket");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert(received_msg);
//alert("Message is received...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
window.onbeforeunload = function(event) {
socket.close();
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
I:これは私のHTMLコードを
public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel>
{
private static final String WEBSOCKET_PATH = "/websocket";
private final SslContext sslCtx;
public WebSocketServerInitializer(SslContext sslCtx) {
this.sslCtx = sslCtx;
}
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
// pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
pipeline.addLast(new WebSocketFrameHandler());
}
}
ですChromeブラウザを使用してページを閲覧し、ウェブページのリンクをクリックすると次のメッセージが表示されます。 ':// localhostを:WSS 4466 /のWebSocket' へ
のWebSocket接続は、ここでいくつかの議論によると、ネット:: ERR_INSECURE_RESPONSE
、および網状のWebSocketのサンプルコードは:接続確立中にエラーが失敗しましたwssはHTTPSで転送する必要があります。私は自分のWebページに次のJavaScriptの文を変更した場合しかし、:
ws = new WebSocket("wss://localhost:4466/websocket");
ws = new WebSocket("wss://echo.websocket.org");
にそれが正常に動作します。それは私が混乱させる、なぜサイトecho.websocket.org httpsなしで動作することができますか?なぜ私のサーバーはできないのですか?私のサーバーのソースコードに何か不足していますか?
PS:次のURLで見つかったecho.websocket.org例:お使いのブラウザは、それが安全で検出されたため、ご使用のWebSocketサーバーに接続するために失敗している
http://jsbin.com/muqamiqimu/edit?js,console
ご理解いただきありがとうございます。 –