2016-07-21 135 views
0

我有一個網頁,試圖連接到一個WebSocket Node.js服務器,它在桌面上可以正常工作,但不能在移動設備上正常工作。JavaScript WebSockets不支持移動設備?

這裏是服務器:

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); 
    } 
} 

就像我說的,一切都運行在桌面上正常但不移動。

+0

什麼手機瀏覽器是您使用 指望再去讀? –

回答

0

你確定線路連接=新的WebSocket(「ws:// localhost:12348」);是正確的?像

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

expecially如果部分的NodeJS是你本地主機....

+1

哦,是啊!這絕對是一個問題! –