试图获得一个示例Node.js应用程序工作

问题描述:

我想获得一个基本的聊天应用程序运行,它使用node.js,express和socket.io。 我已经成功地将所有的东西都运行到了服务器端或客户端没有任何错误的地方。但是,该应用程序不起作用。
我应该能够打开两个浏览器窗口并与我自己聊天。我输入文本框向我发送聊天消息,但没有收到任何消息。我很确定没有发送任何内容。使用FireBug我没有看到任何新的请求通过网络发送。然而,使用Firebug我也看到所有请求的包含文件都被正确地提供给浏览器。只有三个 - index.html,socket.io.js和jquery.min.js。试图获得一个示例Node.js应用程序工作

下面是本教程的链接我使用: http://philmunt.com/post/15643013618/node-js-socket-io-chat-application-tutorial

随着我修改了它一点点地得到它的工作server.js文件。主要的问题是我的浏览器没有收到socket.io.js,所以我添加了一个路径让server.js知道它在哪里。像这样: app.get('/ sio/socket.io.js',function(req,res){........ 这可能是我的问题吗?

这是服务器代码 - server.js:

var express = require('express'), app = express(), http = require('http'), server = http.createServer(app), io = require('socket.io').listen(server); 

// listen for new web clients: 
server.listen(8080); 

app.get('/', function (req, res) { 
res.sendfile(__dirname + '/index.html'); 
});   

app.get('/sio/socket.io.js', function (req, res) { 
res.sendfile('/root/nodejs/node-v0.10.0/node_modules/socket.io/lib/socket.io.js'); 
});  

io.sockets.on('connection', function (socket) { socket.on('sendMessage', function (data) { socket.broadcast.emit('message', data); socket.emit('message', { text: '<strong>'+data.text+'</strong>' }); 
}); 
}); 

下面是index.html文件:

<html> 
    <body> 
    <script src="/sio/socket.io.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script> 
     $(document).ready(function() { 
     var socket = io.connect('http://localhost'); 
     socket.on('message', function (data) { 
      $('#chat').append(data.text + '<br />'); 
     }); 

     $('#send').click(function() { 
      socket.emit('sendMessage', { text: $('#text').val() }); 
      $('text').val(''); 
     }); 

     }); 
    </script> 

    <div id="chat" style="width: 500px; height: 300px; border: 1px solid black"> 

    </div>  

    <input type="text" name="text" id="text"> 
    <input type="button" name="send" id="send" value="send"> 
    </body> 
</html> 

取出app.get(socketstuff)的一部分,你不需要,由于JS文件可用做为你提供

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

并将您的插座变量更改为:

var socket = io.connect();