【问题标题】:Chat App using Socket.io and Node.js使用 Socket.io 和 Node.js 的聊天应用程序
【发布时间】:2013-07-01 17:20:29
【问题描述】:

我正在使用 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 地址和端口号。你能告诉我是什么问题吗?

【问题讨论】:

  • 你能澄清一下吗? “在服务器端运行 .html 文件”是什么意思?你怎么能看到“它没有运行”?您是否有可能忘记确保socket.io.js 在服务器端的路径中?
  • run.html 表示客户端通过提供IP地址在浏览器上运行。
  • socket.io.js 我必须在哪里进行更改?

标签: node.js socket.io putty filezilla


【解决方案1】:

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

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

如果您得到一个空白页面,请检查浏览器 javascript 错误消息。

您可能忘记在该目录“/socket.io/”中上传文件“socket.io.js”。尝试直接下载该文件以查看您的服务器是否提供服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2014-10-18
    • 2016-04-30
    • 2012-08-11
    • 2014-08-13
    • 2015-06-17
    • 2019-11-07
    相关资源
    最近更新 更多