JavaScript WebSockets 无法在移动设备上运行?

标签 javascript mobile tcp websocket

我有一个网页尝试连接到 WebSocket Node.js 服务器,它在桌面上运行良好,但在移动设备上运行不佳。

这是服务器:

var websocket = require("websocket").server;
var http = require("http");

var port = 12348;

var server = http.createServer(function (request, response) {
    //console.log("server > received request for: " + request.url);
    //response.writeHead(404);
    //response.end();
});

server.listen(port, function () {
    console.log("listening on port " + port);
});

socket = new websocket({
    httpServer: server,
});

socket.on("request", function (request) {
    var connection = request.accept(null, request.origin);
    console.log("connection accepted");

    connection.send("Welcome to BrD");
    connection.send('Type "brdhelp" for help');

    connection.on("message", 
    function (data) {
        if (data.type === "utf8") {
            var message = data.utf8Data;

            console.log("client > " + message);
        }
    });

    connection.on("close", 
    function (reasonCode, description) {
        console.log("server > " + connection.remoteAddress + " disconnected.");
    });
});

这是网页上的客户端:

//Enables mozilla's built-in WebSocket if it's available
window.WebSocket = window.WebSocket || window.MozWebSocket;
var pageHeight = window.innerHeight ||document.documentElement.clientHeight || document.body.clientHeight || document.body.offsetHeight;

connectToServer();

function connectToServer () {
    connection = new WebSocket("ws://localhost:12348");

    connection.onopen = 
    function () {
        addResponseLine("connected to server, type \"help\" for client help");
    };

    connection.onerror = 
    function (error) {

    };

    connection.onmessage = 
    function (message) {
        addResponseLine("server > " + message.data);
    };

    connection.onclose = 
    function () {
        addLine("disconnected from server, type \"reconnect\" to reconnect");
    };
}

function checkInput () {
    var event = window.event || event.which;

    //When user presses enter, add input to <p> and clear the input
    if (event.keyCode == 13) {
        event.preventDefault();
        addLine(document.getElementById("textinput").value);
        document.getElementById("textinput").value = "";
    }

    //Set height of input TODO: do this when document loads
    var newHeight = ((pageHeight - 20) - document.getElementById("consoletext").style.height);
    if (document.getElementById("textinput").style.height != newHeight) {
        document.getElementById("textinput").style.height = newHeight + "px";
    }
}

//This function is for outpput to the user, it doesn't check for commands
function addResponseLine (line) {
    var newText = document.createTextNode(line);
    var br = document.createElement("br");
    document.getElementById("consoletext").appendChild(newText);
    document.getElementById("consoletext").appendChild(br);
}

//This function is for adding the user's input to the <p>, it can also check for commands
function addLine (line) {
    if (line != "") {
        line = line.toLowerCase();
        var newText = document.createTextNode(line);
        var br = document.createElement("br");
        document.getElementById("consoletext").appendChild(newText);
        document.getElementById("consoletext").appendChild(br);

        connection.send(line);
    }
}

就像我说的,在桌面上一切正常,但在移动设备上则不然。

最佳答案

您确定该行connection = new WebSocket("ws://localhost:12348");是正确的? 我期望类似的东西

connection = new WebSocket("ws://xxx.yyy.zzz.kkk:12348");

特别是如果 Nodejs 部分位于您的本地主机中......

关于JavaScript WebSockets 无法在移动设备上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509260/

相关文章:

javascript - 输入和选择过滤器如何协同工作?

javascript - Codepen jquery 代码工作正常但是当我将它移到我的 wordpress 网站时它不再工作

android - @font-face 不适用于移动设备(特别是 Android)

linux - 在本地主机上使用相同的 IP 和端口创建套接字

java - 压缩类错误 - 线程异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range: -1

java - 实时游戏服务器最有效的 Java 实现是什么?

javascript - jQuery:为什么这个动画步骤功能不起作用?

javascript - 类型错误 : Cannot read property '0' of undefined when comparing the arrays?

html - 在开发人员工具中测试响应能力时,网站 react 完美,但不是以实际手机屏幕为中心?

javascript - @media 仅屏幕对比。框架