Linux - WebSocket test error

2019-08-14 22:56发布

I am testing on opening websocket and echo. I am trying to run on Linux-based PC and testing with example code in the official websocket website 'http://www.websocket.org/echo.html' I just copied the code there and saved as HTML file. But, when I access '127.0.0.1/websocket.html' , only HTML code is appeared on the screen, no working with JavaScript file. I tried exact same code with my laptop which is window-based, it works correctly. I am using a web browser as "Chromium 10.0.648.133 Ubuntu 10.04" Can you please help me out? The code is here below..

    <!DOCTYPE html>
    <meta charset="utf-8" />
    <title>WebSocket Test</title>
    <script language="javascript" type="text/javascript">
    var wsUri = "ws://echo.websocket.org/";
    var output;

    function init() {
    output = document.getElementById("output");
testWebSocket();


}

function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}

function onOpen(evt) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}

function onClose(evt) {
writeToScreen("DISCONNECTED");
}

function onMessage(evt) {
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
websocket.close();
}

function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}

function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}

function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}

window.addEventListener("load", init, false);

</script>
<h2>WebSocket Test</h2>
<div id="output"></div>
</html>

2条回答
Bombasti
2楼-- · 2019-08-14 23:40

The operating system has absolutely nothing to do with WebSocket working or not. This is a browser issue. Firefox, Chromium or whatever provide the same features to web developers on every platform they support.

Chromium 10 has only partial support for WebSocket, which means it supports an old version of the standard/protocol or you must enable them manually. Just update your browser. Chromium 10 was released in March 2011 and thus is quite old in this current situation where new HTML5 features are added to almost every browser release.

查看更多
Lonely孤独者°
3楼-- · 2019-08-14 23:51

Are WebSockets not supported in Firefox Are WebSockets not supported in Firefox

The above link may be helpful for you...

查看更多
登录 后发表回答