使用Socket.io和Node.js聊天App

时间:2013-07-01 06:27:09

标签: node.js socket.io putty filezilla

我正在使用Node.js和Socket.io开发聊天应用。 这是我的代码。|

socket.js

var io = require('socket.io').listen(8001);
var http = require('http');
var url = require('url');
var fs = require('fs');
// open the socket connection
io.sockets.on('connection', function (socket) {

   // listen for the chat even. and will recieve
   // data from the sender.
   socket.on('chat', function (data) {

      // default value of the name of the sender.
      var sender = 'unregistered';

      // get the name of the sender
      socket.get('nickname', function (err, name) {
         console.log('Chat Message By: ', name);
         console.log('error ', err);
         sender = name;
      });

      // broadcast data recieved from the sender
      // to others who are connected, but not
      // from the original sender.
      socket.broadcast.emit('chat', {
         msg : data,
         msgr : sender
      });
   });

   // listen for user registrations
   // then set the socket nickname to
   socket.on('register', function (name) {

      // make a nickname paramater for this socket
      // and then set its value to the name recieved
      // from the register even above. and then run
      // the function that follows inside it.
      socket.set('nickname', name, function () {

         // this kind of emit will send to all! :D
         io.sockets.emit('chat', {
            msg : "Hello " + name + '!',
            msgr : "Mr.Server"
         });
      });
   });

});

的index.html

<html>
   <head>
        <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
            <script>
               var name = '';
         var socket = io.connect('http://localhost:8001');


         // at document read (runs only ones).
         $(document).ready(function(){


            // on click of the button (jquery thing)
            // the things inside this clause happen only when
            // the button is clicked.
            $("button").click(function(){

               // just some simple logging
               $("p#log").html('Sent message: ' + $("input#msg").val());

               // send message on inputbox to server
               socket.emit('chat', $("input#msg").val() );


               $("p#data_recieved").append("<br />\r\n" + name + ': ' + $("input#msg").val());

               // then we empty the text on the input box.
               $("input#msg").val('');
            });

            $("#btnSubmit").click(function(){
            alert("Disconnected");
            //socket.clients[kickedUserSocketId].onDisconnect();
           socket.close();
    });

            // ask for the name of the user, ask again if no name.
            while (name == '') {
               name = prompt("What's your name?","");
            }

            // send the name to the server, and the server's
            // register wait will recieve this.
            socket.emit('register', name );
         });



         // listen for chat event and recieve data
         socket.on('chat', function (data) {

            // print data (jquery thing)
            $("p#data_recieved").append("<br />\r\n" + data.msgr + ': ' + data.msg);

            // we log this event for fun :D
            $("p#log").html('got message: ' + data.msg);

         });

         socket.emit('forceDisconnect');
      </script>
   </head>
   <body>
      <input type="text" id="msg"></input>
      <button>Click me</button>
      <p id="log"></p>
      <p id="data_recieved"></p>
   </body>

<input id = "btnSubmit" type="submit" value="Disconnect"/>


</html>

我在命令提示符下运行第一个socket.js。之后,我在浏览器中运行.html文件2次。现在有2个用户可以通过浏览器聊天但是,当我试图将我的.js文件和.html文件放在我使用FileZila创建并运行.js文件的服务器上时,它正在运行,但是当我尝试在服务器端运行.html文件时(在本例中为FileZila) ),通过提供服务器的IP地址和端口号,它没有运行。你能告诉我这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

这似乎不是nodejs - 问题。 您有一台服务器应该可以通过IP访问,例如“http://your.domain.or.ip:80”。

如果您的浏览器中出现“无法连接”,请确保没有防火墙,并且可以从浏览器位置(也称为“您的电脑”)访问该端口(例如:80) )。请注意,您在端口8001上有套接字服务,而您的Web服务器可能在不同的端口上运行?确保两个端口都已打开。

如果您看到空白页,请检查浏览器的javascript错误消息。

您可能忘记在'/socket.io/'目录中上传'socket.io.js'文件。尝试直接下载该文件以查看您的服务器是否正常运行。