简体   繁体   English

如何连接到远程Node.js服务器?

[英]How to connect to a remote Node.js server?

I'm using C9.io 我正在使用C9.io

Here my server: 这是我的服务器:

var io = require('socket.io');


  var socket = io.listen(8080, { /* options */ });
  socket.set('log level', 1);


  socket.on('connection', function(socket) {

        console.log("connected");

    socket.on('message1', function(data) {
          socket.emit("message1",JSON.stringify({type:'type1',message: 'messageContent'}));

    });

    socket.on('disconnect', function() {

         console.log("diconnected");

    });
  });

When i run it generate this url: https://xxx-c9-smartytwiti.c9.io and tell me that my code is running in this URL. 当我运行它时,生成以下URL: https : //xxx-c9-smartytwiti.c9.io,并告诉我我的代码正在此URL中运行。

Note : xxx is my workspace 注意 :xxx是我的工作空间

What i did in my client: Connect to " https://xxx-c9-smartytwiti.c9.io:8080/ " .... 我在客户端执行的操作:连接到“ https://xxx-c9-smartytwiti.c9.io:8080/ ”...。

Then i get this error on console(firefox browser): 然后我在控制台(Firefox浏览器)上收到此错误:

cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://xxx-c9-smartytwiti.c9.io:8080/socket.io/1/?t=1406060495041. This can be fixed by moving the resource to the same domain or enabling CORS.

Note : when i host my server locally it works perfectly. 注意 :当我在本地托管服务器时,它可以完美运行。

Seems like c9.io using a proxy or a firewall, but how can i test my code written in the c9.io remotely ? 好像使用代理或防火墙的c9.io,但是如何远程测试用c9.io编写的代码?

UPDATE UPDATE

According to ruben's response i've changed my server, and it works when my socket.io-client hosted in C9 but still can't get this working on remote client(i've also hosted the client in my FTP but same result): 根据ruben的答复,我更改了服务器,并且当我的socket.io-client托管在C9中但仍无法在远程客户端上运行时,它也可以工作(我也将客户端托管在FTP中,但结果相同) :

// module dependencies
var http = require("http"),
    sio  = require("socket.io");

// create http server
var server = http.createServer().listen(process.env.PORT, process.env.IP),

// create socket server
io = sio.listen(server);

// set socket.io debugging
io.set('log level', 1);


io.set('origins', '*:*');


io.sockets.on('connection', function (socket) {


  socket.emit('news', { message: 'Hello world!' });

  socket.on('my other event', function (data) {
    console.log(data.message);
  });

});

Looks like the origin configuration has been ignored, i'm also not sure about C9.io.. 看起来原始配置已被忽略,我也不确定C9.io ..

suggestions ? 建议?

Cheers. 干杯。

Same-origin policy requires that your client code and WebSocket server be hosted at the same URL and port. 同源策略要求您的客户端代码和WebSocket服务器托管在相同的URL和端口上。 You can find specific examples of ways to integrate them in the Socket.IO docs . 您可以在Socket.IO文档中找到将其集成的具体示例。 Here's their example of how to do it using the built-in HTTP server. 这是他们如何使用内置HTTP服务器执行此操作的示例。 Instead of giving Socket.IO a hostname/port, you give it your webserver object: 不用给Socket.IO一个主机名/端口,而是给它一个webserver对象:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

You are using port 8080. Instead try using process.env.IP and process.env.PORT . 您正在使用端口8080。而是尝试使用process.env.IPprocess.env.PORT In addition it is important to not specify a port on the domain to your workspace. 另外,重要的是不要在工作区的域上指定端口。 The default port (port 80) is forward to the internal port of your container on c9.io . 默认端口(端口80)转发到c9.io容器的内部端口。 If you connect to the default port by not specifying it you won't have a problem with cross domain security. 如果通过不指定默认端口连接到默认端口,则跨域安全性不会出现问题。

See also: https://c9.io/site/blog/2013/05/native-websockets-support/ 另请参阅: https : //c9.io/site/blog/2013/05/native-websockets-support/

Ruben - Cloud9 Support Ruben-Cloud9支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM