node.js - Socket.io client does not connect to server -


i trying make node.js app have embedded chat. using socket.io create chat. have attempted set server / client, client not seem connecting. have application set log when sockets connect , disconnect never seems log anything. figured client being retarded opened chrome devtools, , typed in:

var socket = io(); 

oddly, start seeing string of failed requests like:

get http://localhost:3000/socket.io/?eio=3&transport=polling&t=1434334401655-37 404 (not found) 

so i'm sure s server. here server code:

var express = require("express"); var mongoose = require("mongoose"); var io = require("socket.io").listen(app); // var restapi = require("./api");  // set application. var app = express(); app.use(express.static("static"));  // mount rest api // app.use("/rest", restapi);  // default index route. app.get("/", function(req, res) {     res.sendfile(__dirname + "/index.html"); });  // maintian transcript of chat var transcript = []  // route return transcript data json app.get("/transcript", function(req, res) {     res.json(json.stringify(transcript)); });  // simple socket.io chat application  // lets keep reference how many connections have var count = 0; io.sockets.on("connection", function(socket) {     // increment number of clients     count++     // keep reference socket     var current = socket;     // log connection     console.log("connected to: " + current + " @ " + date.now() + ".");     // log current number of users     console.log("there " + count + "users connected.");      // handle disconnection     socket.on("disconnect", function() {         // same routine connect         console.log(current + ": disconnected @ " + date.now() + ".");         console.log("there " + count + "users connected.");     }); });  app.listen(3000); 

here html client:

<!doctype html> <html>     <head>         <meta name="viewport">         <title>paint</title>         <!-- <script src="/js/application.js"></script> -->         <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>     </head>     <body>         <header></header>         <nav></nav>         <aside>             <div id="toolbox"></div>             <!-- display: none; default -->             <div id="chat"></div>              <div class="buttons">                 <div class="toolboxbutton">toolbox</div>                 <div class="chatbutton">chat</div>             </div>         </aside>         <section></section>         <footer></footer>         <script>             var socket = io();         </script>     </body> </html> 

why can't client connect?

i don't know if issue, you're calling this:

var io = require("socket.io").listen(app); 

before assign app variable undefined.


the request url:

http://localhost:3000/socket.io/?eio=3&transport=polling&t=1434334401655-37 

is how socket.io connection starts normal. problem socket.io listener wasn't running on server nothing listening route.


also, don't know if this:

io.sockets.on("connection", function(socket) {...}); 

works in socket.io v1+. socket.io doc says use this:

io.on('connection', function(socket) {...}); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -