0

I'm trying to make real time chat with nodeJs.. But i have some problems. The console.log in the part of

io.on('connection', function(socket)
    {
        console.log("A user connected");
    });

does not console.log at all.

When I got stuck i was searching for a guide.. I build it almost the same as in the guide.

Here is my html page:

<!doctype html>
<html>
    <head>
        <title>
            Socket.IO chat
        </title>
        <link rel="stylesheet" href="./CSS/style.css" media="screen" title="no title" charset="utf-8">
    </head>
    <body>
        <ul id="messages">

        </ul>
        <form action="">
            <input id="m" autocomplete="off" />
            <button>
                Send
            </button>
        </form>

         <script src="/socket.io/socket.io.js"></script>
        <script>
            var socket = io();
        </script>

    </body>
</html>

And here my js file:

var app     = require("express")(),
    http    = require("http").Server(app),
    io      = require("socket.io")(http),
    port    = 3000;


    app.get('/', function(req, res)
    {
        res.sendFile(__dirname + "./index.html");
    });

    io.on('connection', function(socket)
    {
        console.log("A user connected");
    });


    http.listen(port, function()
    {
        console.log('Listening on port: ' + port);
    });

my file structure:

enter image description here

^because I think it has something to do with the linking between the pages.

I am using this guide: http://socket.io/get-started/chat/

Thanks in advance, I hope to get some answers so i can go on with this :) Cheers

kkuilla
  • 2,226
  • 3
  • 34
  • 37
Sam
  • 75
  • 2
  • 12
  • Go to index.js's folder in cmd, then run "node index.js". After doing that, do you see "listening on port: 3000" in cmd or do you see an error? – Jonah Oct 02 '15 at 08:34
  • yes i can see the "listening on port 3000" in my console.. no error at all – Sam Oct 02 '15 at 08:35
  • Anything show up in the browser's console after going to localhost:3000 in browser? – Jonah Oct 02 '15 at 08:37
  • i get this: http://i.imgur.com/ZYpjVdV.png – Sam Oct 02 '15 at 08:37
  • It must be that socket.io wasn't installed correctly. When you ran "npm install -- save socket.io", are you sure you were inside the correct folder? Run it again inside the folder that contains index.js just to be sure. – Jonah Oct 02 '15 at 08:39
  • yea.. when i installed socket.io i got some errors so i was not sure about it if it was OK.. – Sam Oct 02 '15 at 08:41
  • sec i make new install and screenshot it – Sam Oct 02 '15 at 08:41
  • this are the errors: http://i.imgur.com/sOMEzl1.png – Sam Oct 02 '15 at 08:42
  • http://i.imgur.com/RWbSyHG.png – Sam Oct 02 '15 at 08:42
  • Look here: https://stackoverflow.com/questions/21365714/nodejs-error-installing-with-npm – misko321 Oct 03 '15 at 11:48

1 Answers1

0

It looks like your HTML page is missing the server URL:

var socket = io.connect("http://" + window.location.host);  //e.g. http://localhost:3000
arnold
  • 735
  • 1
  • 7
  • 14