javascript - Socket.io error - ERR_CONNECTION_TIMED_OUT -


i trying build basic chat html program. have set node server test it, however, when accessing html page, socket error. it's first time working node.js , setting node server, i've surely done wrong.

thanks who's taking @ this!

var express = require('express');  var app = express();  var server = require('http').createserver(app);  var io = require("socket.io").listen(server);    var socket = io.listen(1223, "1.2.3.4");   server.listen(process.env.port || 3000);    console.log('server running...');  var people = {};      app.get('/', function(req, res){  	res.sendfile(__dirname + '/index.html');  })    // when connecting  socket.on("connection", function(client) {  	client.on("join", function(name){  		people[client.id] = name;  		client.emit("update", "you have connected server.");  		socket.sockets.emit("update", name + " has joined server.");  		socket.sockets.emit("update-people", people);  	});    // when sending  	client.on("send", function(msg){  		socket.sockets.emit("chat", people[client.id], msg);  	});    // when disconnecting  	client.on("disconnect", function(){  		socket.sockets.emit("update", people[client.id] + " has left server.");  		delete people[client.id];  		socket.sockets.emit("update-people", people);  	});  });
<!doctype html>    <html lang="en">  <head>      <!-- latest compiled , minified css -->         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>      <script src="/socket.io/socket.io.js"></script>  </head>      <script>                $(document).ready(function(){            var socket = io.connect("1.2.3.4:1223");          $("#chat").hide();          $("#name").focus();          $("form").submit(function(event){              event.preventdefault();          });                      $("#join").click(function(){              var name = $("#name").val();              if (name != "") {                  socket.emit("join", name);                  $("#login").detach();                  $("#chat").show();                  $("#msg").focus();                  ready = true;              }          });             $("#name").keypress(function(e){              if(e.which == 13) {                  var name = $("#name").val();                  if (name != "") {                      socket.emit("join", name);                      ready = true;                      $("#login").detach();                      $("#chat").show();                      $("#msg").focus();                  }              }          });              socket.on("update", function(msg) {              if(ready)                  $("#msgs").append("" + msg + "");          })             socket.on("update-people", function(people){              if(ready) {                  $("#people").empty();                  $.each(people, function(clientid, name) {                      $('#people').append("" + name + "");                  });              }          });            socket.on("chat", function(who, msg){              if(ready) {                  $("#msgs").append("" + + " says: " + msg + "");              }          });            socket.on("disconnect", function(){              $("#msgs").append("the server not available");              $("#msg").attr("disabled", "disabled");              $("#send").attr("disabled", "disabled");          });    // sending message (either button click or enter)          $("#send").click(function(){              var msg = $("#msg").val();              socket.emit("send", msg);              $("#msg").val("");          });            $("msg").keypress(function(e){            if (e.which == 13) {              var msg = $("msg").val();              socket.emit("send", msg);              $("#msg").val("");            }          });          });        </script>    <body>    <div class="container">    <div class="row">          <div class="span2">            <ul id="people" class="unstyled"></ul>          </div>          <div class="span4">            <ul id="msgs" class="unstyled">          </div>        </div>          <div class="row">          <div class="span5 offset2" id="login">            <form class="form-inline">              <input type="text" class="input-small" placeholder="your name" id="name">              <input type="button" name="join" id="join" value="join" class="btn btn-primary">            </form>          </div>          <div class="span5 offset2" id="chat">            <form id="2" class="form-inline">              <input type="text" class="input" placeholder="your message" id="msg">              <input type="button" name="send" id="send" value="send" class="btn btn-success">            </form>          </div>        </div>          </div>      </body>  </html>

you mixed different listen calls.

var server = require('http').createserver(app); var io = require("socket.io").listen(server); var socket = io.listen(1223, "1.2.3.4");  server.listen(process.env.port || 3000); 

the third line has not effect expect. io not listening itself. uses http server listening on port 3000.

rather use on server

var server = require('http').createserver(app); var socket = require("socket.io").listen(server); server.listen(process.env.port || 3000);    

and in client connect port 3000 (or port configured via port environment variable.

var socket = io.connect("your.domain:3000"); 

if want listen on dedicated port socket.io, have create second http server listening on second port, , bind 1 socket.io.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -