调用send时,关闭的WebSocket应该如何表现?

时间:2015-11-16 10:59:19

标签: javascript websocket

WebSocket.send OPEN中,它说:

  

抛出异常
  的 INVALID_STATE_ERR
  该连接目前不是CONNECTING

CLOSED状态下确实抛出异常,但如果套接字已关闭(状态为var s = null; function log(msg) { var d = document.createElement('div'); d.textContent = (new Date().toLocaleTimeString()) +": " + msg; document.getElementById('out').appendChild(d); } function create() { log("Creating"); s = new WebSocket("wss://echo.websocket.org"); s.onopen = function() { log("Socket opened"); } s.onmessage = function(e) { log("Got: " + e.data); } send("before open"); } function send(msg) { try { log("Sending: " + msg); s.send(msg); log("Send didn't throw"); } catch(e) { log("Exception: " + e.message); } } function close() { log("Closing"); s.close(); } document.getElementById('create').addEventListener('click', create, true); document.getElementById('send').addEventListener('click', send.bind(null, 'manually'), true); document.getElementById('close').addEventListener('click', close, true);),那么我不会得到任何异常。对于我尝试过的所有浏览器都会发生这种情况。

我试过阅读MDN documentation,但似乎找不到任何相关内容。

这是一些复制代码:

  1. 单击创建也会在套接字打开之前发送(抛出异常)
  2. 套接字打开后,点击发送(不会抛出异常)
  3. 点击关闭然后发送(不会抛出异常,这让我感到惊讶)
  4. <button id="create">Create</button> 
    <button id="send">Send</button>
    <button id="close">Close</button>
    <div id="out"/>
    {{1}}

0 个答案:

没有答案