Saturday, 29 April 2017

Socket.io does not work

I've installed socket.io through the "npm install --save socket.io" and tried to use in the following way;

var express = require('express');
var app = express()
var router = express.Router();
var guestbookData = require('./guestbook.json');
var bodyParser = require('body-parser');
var fs = require('fs');
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

Then I added the following into my html file's header part;

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

However, when I start the application and reload the web page, socket.io does not print 'a user connected' to the console. My console doesn't show any sign of error and the website works normal, so I have truly no idea what is wrong.

I'd be grateful if you can help me.



via ricster

No comments:

Post a Comment