0

actually i'm trying to see if a client is connected or no with socket.io. but in the console nothing appears excepte that the server is working.

//server.js

var http = require('http');
var fs = require('fs');
// Chargement du fichier index.html affiché au client
var server = http.createServer(function(req, res) {
  fs.readFile('./index.html', 'utf-8', function(error, content) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.end(content);
  });
});


// Chargement de socket.io

var io = require('socket.io').listen(server);
// Quand un client se connecte, on le note dans la console
io.sockets.on('connection', function (socket) {
  console.log('Un client est connecté !');
});
server.listen(8080);

//client index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Socket.io</title>
    </head>
    <body>
        <h1>Communication avec socket.io !</h1>
        <script src="/socket.io/socket.io.js"></script>
        <script>
            var socket = io.connect('http://localhost:8080');
        </script>
    </body>
</html>
1
  • i try this code and a saw (in the server log) that a user is connect when he was enter the page http://localhost:8080 Commented Mar 16, 2016 at 8:50

1 Answer 1

1

I am using express api from socket.io ,

var express = require('express') ;
var app = express() ;
var server = require('http').createServer(app) ;
var socket = require('socket.io')(server) ;

Now , you need to say server to listen the port . (the port number you client is trying to connect here is 8080 ) In server script say ,

server.listen(8080) ;

Now , install express module from terminal window manually or just put a dependency file and install

Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't work :/ can you please post the whole server.js for me ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.