2013-07-01 69 views
0

我正在開發如何使用Node.js和Socket.io聊天應用。 這是我的代碼。|聊天應用程序中使用Socket.io和Node.js的

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地址和端口號未運行。你能告訴我有什麼問題嗎?

+0

你能澄清一下嗎? 「在服務器端運行.html文件」是什麼意思?你怎麼看到「它沒有運行」?是否有機會忘記確保'socket.io.js'在服務器端的路徑中? – CFrei

+0

run.html表示客戶端將通過提供IP地址在瀏覽器上運行它。 –

+0

socket.io.js我必須進行更改嗎? –

回答

0

這似乎並不成爲一個nodejs -issue。 您有應該可以訪問通過IP的東西,如「http://your.domain.or.ip:80」的服務器。

如果您的瀏覽器中出現「無法連接」,請確保沒有防火牆,並且該端口(例如:80)可從您的瀏覽器位置訪問(又稱爲「您的PC 「)。請注意,您在端口8001上使用套接字服務,並且您的Web服務器可能在另一個端口上運行?確保兩個端口都打開。

如果您收到一個空白頁,請檢查瀏覽器JavaScript錯誤消息。

您有可能忘記上傳'/socket.io/'目錄中的文件'socket.io.js'。嘗試直接下載該文件以查看您的服務器是否提供服務。

相關問題