Windows 8应用程序和nodejs websocket没有相互连接

时间:2014-07-17 10:28:15

标签: javascript html5 node.js websocket windows-applications

我正在使用Windows应用程序并尝试将其连接到节点服务器 我在windows应用程序中使用Web套接字,并从npm使用'websocket'作为节点 我很难连接这两个似乎有连接但是当我尝试发送信息时(简单的hello world string)没有任何反应。
我有一个简单的JavaScript Windows应用程序 在我的default.html中只有一个简单的按钮:

 <button onclick="Check()">Check status</button>

在default.js中我有Check功能:

function Check()
{
    var host = "ws://192.168.201.91:8080";
    try
    {
        socket = new WebSocket(host);
        socket.onopen = function (openEvent)
        {
            console.log("Sockets open");
            socket.send("Hello, world");
            console.log("Socket state: " + socket.readyState);
            console.log("Message is sent to: " + socket.url);
        };
        socket.onerror = function (errorEvent)
        {
            console.log(" 'WebSocket Status:: Error was reported';")
        };
        socket.onclose = function (closeEvent)
        {
                 console.log("WebSocket Status:: Socket Closed");
        };
        socket.onmessage = function (messageEvent)
        {
            console.log(socket.toString);
            var received_msg = messageEvent.data;
            console.log("Message recieved: " + received_msg);
        }
    }
    catch(exception)
    {
        if (window.console)
            console.log(exception);
    }
}

socket.readyState返回1表示已建立连接且套接字已准备好发送! 我的节点服务器如下所示:

#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

wsServer = new WebSocketServer({
    httpServer: server,

    // You should not use autoAcceptConnections for production
    // applications, as it defeats all standard cross-origin protection
    // facilities built into the protocol and the browser.  You should
    // *always* verify the connection's origin and decide whether or not
    // to accept it.
    autoAcceptConnections: true
});
console.log("Here wsServer");
function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed.
  return true;
}

wsServer.on('request', function(request) {
    //if (!originIsAllowed(request.origin)) {
     // Make sure we only accept requests from an allowed origin
      //request.reject();
     //  console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
     //  return;
    //}
    console.log("Here");
    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }

    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

我已经取消了任何安全检查,因为我只想将消息发送到服务器。 但这不会发生,我不知道为什么!任何帮助将不胜感激。

编辑 - 只是认为它应该可以发送信息,看它是否到达服务器但我只是不确定在Windows应用程序是什么IP!如果我可以告诉我的节点服务器发送至少让我知道某个频道是开放的信息。 任何想法如何通过JavaScript获取Windows应用程序的IP?

0 个答案:

没有答案