在Javascript客户端中处理不成功的websocket升级请求 [英] Handling unsuccessful websocket upgrade requests in Javascript Client

查看:212
本文介绍了在Javascript客户端中处理不成功的websocket升级请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让javascript客户端进程获取服务器返回的HTTP状态代码,当客户端发出websocket升级请求并且该请求不成功时。

I want to have a javascript client process get the HTTP status code that a server is returning when the client makes a websocket upgrade request and that request is unsuccessful.

我的服务器返回HTTP 400表示websocket升级失败。

I have my server returning HTTP 400 to indicate that a websocket upgrade is unsuccessful.

我正在使用谷歌Chrome和当我打开开发者控制台时,我可以看到以下消息:

I am using Google Chrome and when i open the developer console I can see the following message:


与'wss://'的WebSocket连接失败:错误期间WebSocket握手:意外的响应代码:400

但是,onerror处理程序不包含此消息,它收到1006错误但不是表示由于获取HTTP 400而发生关闭。

However, the onerror handler does not contain this message, it receives a 1006 error but does not indicate that the closure occured as a result of getting HTTP 400.

javascript开发人员如何处理握手错误?我想在收到握手错误时向客户提供信息性消息。

How does a javascript developer handle handshake errors? I would like to provide the client with an informative message when they get a handshake error.

我已将下面的websocket错误,它似乎不包含任何我可以用来指示此错误是websocket握手错误的结果。

I have put the websocket error below, it does not seem to contain anything that I can use to indicate that this error is a result of a websocket handshake error.

 Websocket Error: {"path":{"length":0},"cancelBubble":false,"returnValue":true,"srcElement":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"defaultPrevented":false,"timeStamp":1417828938039,"cancelable":false,"bubbles":false,"eventPhase":2,"currentTarget":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"target":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"type":"error"}


推荐答案

恐怕没办法来自J. avascript知道谈判的HTTP状态代码。

I am afraid there is no way from Javascript to know the HTTP status code of the negotiation.

定义关闭代码,1006仅表示连接突然关闭,但协议甚至允许在不提供原因的情况下关闭连接。这与 readyState API一起是唯一的你必须诊断出问题原因的工具。

There are defined closing codes, and 1006 only means that the connection is closed abruptly, but the protocol even allows to close the connection without providing a reason. That, together with the readyState API, are the only tools you have to diagnosed the reason of the problem.

这篇关于在Javascript客户端中处理不成功的websocket升级请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆