JavaScript WebSockets无法在移动设备上工作



我有一个网页试图连接到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);
    }
}

就像我说的,在桌面上一切都很好,但在手机上不行。

您确定线路连接=新的WebSocket("ws://localhost:12348");是正确的吗?我期待类似的东西

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

特别是如果nodejs部分在本地主机中。。。。

相关内容

  • 没有找到相关文章

最新更新