只要我没有超过连接数,我的websocket代码就可以正常工作。我想在503错误发生时陷阱。显示的控制台输出如下:
SCRIPT12008:WebSocket错误:HTTP响应不正确。状态代码503,活动WebSocket请求数已达到允许的最大并发WebSocket请求。
WebSocket.OnError处理程序不会显示完整的细节。如何获得上面控制台输出中显示的详细程度?某种文件收集,或者,我真的不知道。
此处为完整代码。套接字服务器处于活动状态,从12/6开始,故意限制为4个连接。以后会把它砍掉。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles/base.css" />
<title>Yielding Loop</title>
<script type="text/javascript">
var c = 0;
var t;
var timer_is_on = 0;
var m_connection = null;
var m_socket_address = "ws://edgeplay.azurewebsites.net/ws";
function timedCount()
{
echoTest();
document.getElementById( "txt" ).value = c;
outmsg( "tick=" + c );
c = c + 1;
t = setTimeout( function () { timedCount() }, 3000 );
}
function startCount()
{
if ( !timer_is_on )
{
timer_is_on = 1;
timedCount();
}
}
function stopCount()
{
clearTimeout( t );
timer_is_on = 0;
}
function SocketInit()
{
try
{
if ( "WebSocket" in window )
{
outmsg( "WebSocket supported, gtg." );
}
else
{
outmsg( "WebSocket NOT SUPPORTED, bailing out" );
return 0;
}
outmsg( "connecting to " + m_socket_address );
try
{
m_connection = new WebSocket( m_socket_address );
}
catch ( err )
{
outmsg( "error creating new socket - " + err );
outmsg( "error.name:" + err.name );
outmsg( "error.message:" + err.message );
}
m_connection.onopen = function ( openEvent )
{
outmsg( "WebSocket.OnOpen - GTG: " + m_socket_address );
};
m_connection.onmessage = function ( messageEvent )
{
outmsg( 'WebSocket.onmessage - Server Reply:: ' + messageEvent.data );
};
m_connection.onerror = function ( errorEvent )
{
outmsg( "WebSocket Status:: Error was reported." );
outmsg( "WebSocket errorEvent: " + errorEvent );
outmsg( "Stopping count" );
var h1 = document.getElementById( "h1Main" );
h1.style = "background-color:red;";
stopCount();
};
m_connection.onclose = function ( closeEvent )
{
outmsg( "WebSocket Status:: Socket Closed" );
outmsg( "closeEvent.code:" + closeEvent.code );
outmsg( "closeEvent.data:" + closeEvent.data );
};
}
catch ( exception )
{
outmsg( "exception: " + exception );
}
}
var m_input = null;
var m_querystring = null;
function echoTest()
{
try
{
m_input = m_input == null ? document.getElementById( "textEcho" ) : m_input;
m_connection.send( m_input.value + location.search );
}
catch ( err )
{
outmsg( "eating error " + err );
}
}
var m_out = null;
var outd = null;
function outmsg( msg )
{
m_out = ( m_out == null ) ? document.getElementById( "divOutput" ) : m_out;
outd = new Date();
m_out.innerHTML = outd.toLocaleTimeString() + ": " + msg + "<br/>" + m_out.innerHTML;
}
</script>
</head>
<body onload="SocketInit();startCount();">
<h1 id="h1Main">Yielding Loop</h1>
<button onclick="SocketInit();startCount();">Init socket and start count</button>
<input type="text" id="txt">
<button onclick="stopCount()">Stop count!</button>
<br/>
<span>text to echo: </span><input type="text" value="BLUE" id="textEcho"/>
<div id="divOutput" style="background-color:antiquewhite; border:3px; width:900px;">
ready....
</div>
<hr />
</body>
</html>
失败时引发的错误:
调试到引发的错误。在那里没有多少善良。输出线如下。逆向计时。
1:04:59 PM: --- closeEvent.reason:
1:04:59 PM: --- closeEvent.data:undefined
1:04:59 PM: --- closeEvent.code:1006
1:04:59 PM: WebSocket.onClose
1:04:59 PM: --- Stopping count
1:04:59 PM: --- errorEvent.type: error
1:04:59 PM: --- errorEvent: [object Event]
1:04:59 PM: WebSocket.onerror
答案 0 :(得分:0)
显然,出于安全考虑,您只会获得1006 onclose。不要让我高兴,但这就是RFC的编写方式。 https://html.spec.whatwg.org/multipage/web-sockets.html#network
用户代理不得向网站中的脚本传达任何失败信息 允许脚本区分以下情况的方式: 无法解析主机名的服务器。一个服务器 数据包无法成功路由。拒绝的服务器 指定端口上的连接。服务器无法正确使用 执行TLS握手(例如,服务器证书不能 验证)。没有完成打开握手的服务器(例如, 因为它不是WebSocket服务器)。发送的WebSocket服务器 正确的打开握手,但指定的选项导致 客户端断开连接(例如服务器指定了一个 客户未提供的子协议)。一个WebSocket服务器 成功完成后突然关闭连接 打开握手。在所有这些情况下,WebSocket 根据WebSocket的要求,连接关闭代码为1006 协议规范。 [WSP]