Node.js和socket.io在Cloud9 IDE上不起作用 [英] Node.js and socket.io don't work on Cloud9 IDE

查看:99
本文介绍了Node.js和socket.io在Cloud9 IDE上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在Cloud9 IDE上运行Node.js和socket.io有经验吗?

Does anyone has experience to have Node.js and socket.io working on Cloud9 IDE?

示例(带有Socket.io的NodeJS) (位于 https://c9.io/site/blog/2013 / 05 / native-websockets-support / )不起作用。

The "Example (NodeJS with Socket.io)" (at https://c9.io/site/blog/2013/05/native-websockets-support/) doesn't work.

首先,服务器( https://c9.io/etlolap/webapp ,/ test.js)会引发错误,除非我进行如下修复。当test.js处于活动选项卡上时,我单击了运行按钮。

First, the server (https://c9.io/etlolap/webapp, /test.js) throws an error unless I fix as follow. I clicked Run button while test.js is on active tab.

var 
  socketIo = require('socket.io'),
  io = socketIo.listen(Number(process.env.PORT));
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
  console.log(data);
  });
});

然后,我的客户( https://c9.io/etlolap/webapp ,/ test.html)仍然无法连接。在test.html处于活动标签上时,我单击了预览按钮。

Then, my client (https://c9.io/etlolap/webapp, /test.html) still cannot connect. I clicked Preview button while test.html is on active tab.

<!doctype html>
<html>
  <head>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('https://webapp-c9-etlolap.c9.io');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
  </head>
  <body>
    Loading...
  </body>
</html>

,并在下面显示错误消息。

and got error message below.


失败加载资源:服务器响应状态为404 ---(未找到) https ://c9.io/socket.io/socket.io.js

未捕获的ReferenceError:未定义io --- test.html:6

Uncaught ReferenceError: io is not defined --- test.html:6


推荐答案

感谢Damhat和Matthias的反馈。经过多次失败的尝试,最终我自己找到了解决方案。
在Cloud9 IDE上,客户端的典型行(此处为test.html)必须更改为

Thanks for feedback from damphat and Matthias. After many failed attempts, finally I figured out the solution myself. On Cloud9 IDE, the typical line in client (test.html here) has to be changed from,

 <script src="/socket.io/socket.io.js"></script>

 <script src="https://webapp-c9-etlolap.c9.io/socket.io/socket.io.js"></script>

前缀是Cloud9项目URL的URL。通过更改此行,我的示例成功了。

The prefix is the URL of your Cloud9 project URL. By changing this line, my example worked.

这篇关于Node.js和socket.io在Cloud9 IDE上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆