-1

I'm trying to use an html page in order to test the connection with my server.

I've got no problem with my server.js

var http = require('http');
var url = require("url");
// Chargement de socket.io
var io = require('socket.io').listen(server);



var server = http.createServer(function(req, res) {
    var page = url.parse(req.url).pathname;

    // 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);

My client should is an html page here

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Socket.io</title>
    </head>

    <body>
    <h1>Communication test with socket.io !</h1>


    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="http://localhost:8000/server_node/node_modules/socket.io/lib/socket.js"></script>
    <script>
        var socket = io.connect('http://localhost:8080');
    </script>
    </body>
</html>

Problem is, when I load the html client, I've got this error

socket.js:6 Uncaught ReferenceError: require is not defined at socket.js:6

I'm not trying to implement a real client, I just want a simple page in order to do some connection test.

Thanks for your help

EDIT: Problem fixed, it was all because I wasn't loading the html file from the serverlike this fs.readFile('./ClientTest.html', 'utf-8', function(error, content) {

As this is just used to do some test, it's fine if it works this way; the client side will be with an other platform.

Sorry for that useless issue :(

Minirock
  • 628
  • 3
  • 13
  • 28

1 Answers1

1

You appear to be importing the server-side library on the client, you want to be importing the client-side one.

As per the docs, this is automatically served when socket.io runs on the server which you can import via

<script src="/socket.io/socket.io.js" />
James
  • 80,725
  • 18
  • 167
  • 237
  • I tried this already, but then I've got this one >> GET file:///socket.io/socket.io.js net::ERR_FILE_NOT_FOUND – Minirock May 14 '19 at 12:21
  • @Minirock have you tried [this answer](https://stackoverflow.com/a/25527751/10429036)? – Oliver Nybo May 14 '19 at 12:34
  • @OliverNybo Yesbut still it's not working.Thing is there is no socket.io.js file in the socket.io folder. – Minirock May 14 '19 at 12:42
  • And did you do `npm instsall socket.io --save`? The **--save** is important. – Oliver Nybo May 14 '19 at 12:43
  • 1
    @Minirock that would suggest to me that you are running this locally (i.e. you are not serving the HTML via the Node server). Have you tried `http://localhost:8000/socket.io/socket.io.js` in that case? – James May 14 '19 at 13:20