内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Websockets not working - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Websockets not working - Stack Overflow

programmeradmin0浏览0评论

I have the following code in javascript:

function ConnectWebSocket() {
 if ("WebSocket" in window) {
    myWebsocket = new WebSocket("wss://myserver/mychannel");
    myWebsocket.onmessage = function(evt) {
        alert("onmessage");
    }
    myWebsocket.onopen = function() {
        alert("onopen");
        myWebsocket.send("msg0");
        myWebsocket.send("msg1");
        myWebsocket.send("msg2");
    }
    myWebsocket.onclose = function() {
        alert("onclose");
        ConnectWebSocket();
    }
  } else {
    // Do something if there is no websockets support
  }
}
ConnectWebSocket();

The problem is that in Firefox, the connection is closed after sending the messages, and reopened due to the mand on the onclose event. If I try to send only one message on onopen, the connection keeps opened, but if I try to send more than one message, the connection shut down. This issue appears only in Firefox, not in Chrome, not in IE, not in Safari.

Can someone help me? In other browsers like IE or Chrome, once the connection is created, it keep opened until I leave the page. I have the 40.0.3v of Firefox

I have the following code in javascript:

function ConnectWebSocket() {
 if ("WebSocket" in window) {
    myWebsocket = new WebSocket("wss://myserver/mychannel");
    myWebsocket.onmessage = function(evt) {
        alert("onmessage");
    }
    myWebsocket.onopen = function() {
        alert("onopen");
        myWebsocket.send("msg0");
        myWebsocket.send("msg1");
        myWebsocket.send("msg2");
    }
    myWebsocket.onclose = function() {
        alert("onclose");
        ConnectWebSocket();
    }
  } else {
    // Do something if there is no websockets support
  }
}
ConnectWebSocket();

The problem is that in Firefox, the connection is closed after sending the messages, and reopened due to the mand on the onclose event. If I try to send only one message on onopen, the connection keeps opened, but if I try to send more than one message, the connection shut down. This issue appears only in Firefox, not in Chrome, not in IE, not in Safari.

Can someone help me? In other browsers like IE or Chrome, once the connection is created, it keep opened until I leave the page. I have the 40.0.3v of Firefox

Share Improve this question edited Sep 28, 2015 at 10:09 Manel asked Sep 22, 2015 at 15:44 ManelManel 591 gold badge1 silver badge5 bronze badges 6
  • check this http://stackoverflow./questions/31627450/websocket-fails-in-firefox – kakajan Commented Sep 22, 2015 at 16:35
  • Hello kakajan, this link is good for me, but it is a custom action. I need that it could be setted by code. Is it possible?Could you help me? THank you. – Manel Commented Sep 28, 2015 at 14:18
  • The WebSockets API (and the underlying protocol) are still in active development, and there are many patibility issues across browsers at this time (and even among different releases of the same browser). I get this from mozilla's website Check the link, there is some WebSocket examples – kakajan Commented Sep 28, 2015 at 16:15
  • And in which language did you write the server side of websocket? Java? – kakajan Commented Sep 28, 2015 at 16:16
  • Can you try sending messages outside of .onopen function? I am not sure, but I think the problem occurs when you send messages in .onopen function. Same issue on this question too – kakajan Commented Sep 28, 2015 at 16:22
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Try this example:

var url = "ws://echo.websocket";

if (!window.WebSocket) alert("WebSocket not supported by this browser");

var myWebSocket = {
    connect: function () {
        var location = url
        this._ws = new WebSocket(location);
        this._ws.onopen = this._onopen;
        this._ws.onmessage = this._onmessage;
        this._ws.onclose = this._onclose;
        this._ws.onerror = this._onerror;
    },

    _onopen: function () {
        console.debug("WebSocket Connected");
    },

    _onmessage: function (message) {
        console.debug("Message Recieved: " + message.data);
    },

    _onclose: function () {
        console.debug("WebSocket Closed");
        kiosk.connect();
    },

    _onerror: function (e) {
        console.debug("Error occured: " + e);
    },

    _send: function (message) {
        console.debug("Message Send: " + message);
        if (this._ws) this._ws.send(message);
    }
};

myWebSocket.connect();
setInterval(function() {
    myWebSocket._send('msg1');
}, 5000);

Here is a JSFidlle

It may be that your support var is not behaving as you expect. The following code works in FireFox without closing the connection:

function ConnectWebSocket() {

    if ("WebSocket" in window) {
        myWebsocket = new WebSocket("ws://echo.websocket/");
        myWebsocket.onmessage = function (evt) {
            alert("onmessage");
        }
        myWebsocket.onopen = function () {
            alert("onopen");
            myWebsocket.send("a test message");
        }
        myWebsocket.onclose = function () {
            alert("onclose");
            ConnectWebSocket();
        }
    } else {
        // Do something if there is no websockets support
    }
}
ConnectWebSocket();

Example Fiddle


  • You can use the tool on Websocket to make sure websockets are working correctly in your browser.
  • Or (although your issue is with FF) you can use the steps listed here to debug websockets.

Try it.

var WS = window.WebSocket || window.MozWebSocket;

if (WS){
    var websocket = new WS("wss://myserver/mychannel");
}
发布评论

评论列表(0)

  1. 暂无评论