WebSocket connection to 'ws://localhost:8080/xxx' failed: Error during WebSocket handshake...

WebSocket connection to ‘ws://localhost:8080/xxx’ failed: Error during WebSocket handshake: Unexpected response code: 200
websocket项目启动之后,页面控制台报错,后面发现是后台加了拦截器导致的,分享解决前后的代码情况。
(本文章分享在CSDN平台,更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217)
ScoketConfig类:

    ...

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/webSocket").setAllowedOrigins("*")
            .addInterceptors(new MyHandshakeInterceptor());
        registry.addHandler(handler(), "/webSocket/socketJs").setAllowedOrigins("*")
        .addInterceptors(new MyHandshakeInterceptor()).withSockJS();
    }

    //静态内部类-拦截器
    private static class MyHandshakeInterceptor implements HandshakeInterceptor{
        ...
    }

去掉拦截器后,ScoketConfig类::

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/webSocket");
        registry.addHandler(handler(), "/webSocket/socketJs").withSockJS();
    }
已标记关键词 清除标记
我的客户端脚本和服务器之间没有WebSocket连接,它在我当前的环境中运行得很好。同时,我也遵循这个<a href="http://socketo.me/docs/push" rel="nofollow noreferrer">链接</a>。 下面是一个服务器脚本,它初始化WebSocket服务器并让客户端连接到了8080端口。</p> <pre><code>public function run() { $loop = Factory::create(); $pusher = new Pusher; $context = new Context($loop); $pull = $context->getSocket(ZMQ::SOCKET_PULL); $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself $pull->on('message', array($pusher, 'onBlogEntry')); // Set up our WebSocket server for clients wanting real-time updates $webSock = new Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect $webServer = new IoServer( new HttpServer( new WsServer( new WampServer( $pusher ) ) ), $webSock ); $loop->run(); </code></pre> <p>这是客户端脚本:</p> <pre><code>var conn = new ab.Session('ws://localhost:8080', function() { /* subscribe to following topics */ conn.subscribe('new_order', function(topic, data) .. </code></pre> <p>同样,这在本地设置中运行良好。</p> <p>还要注意的是,我的应用程序是使用来自docker容器的指定端口承载的。</p> <pre><code>http://192.168.12.52:8094/xyz/new/.... </code></pre> <p>我还尝试在客户端脚本中指定IP:</p> <pre><code>var conn = new ab.Session('ws://192.168.12.52:8080', function() { /* subscribe to following topics */ conn.subscribe('new_order', function(topic, data) ... </code></pre> <p>在这种情况下,我会得到以下错误:</p> <pre><code>WebSocket connection to 'ws://192.168.11.32:8080/' failed: Error during WebSocket handshake: Unexpected response code: 403 </code></pre> <p>这里缺少什么?</p> </div>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页