0

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 人のユーザーがブラウザ経由でチャットできるようになりました。しかし、FileZila を使用して作成した .js ファイルと .html ファイルをサーバーに配置しようとすると、.js ファイルは実行されますが、サーバー側で .html ファイルを実行しようとすると (この場合は FileZila ) 、実行されていないサーバーの IP アドレスとポート番号を指定します。何が問題なのか教えていただけますか?

4

1 に答える 1