0

I have a path http://localhost:3000/home/ to which i wanted to create a socket connection. my index.js

var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require("path");
var io = require('socket.io')(http);


app.get('*', function (req, res){
  res.sendFile(path.join(__dirname, '/Public'));
});

app.use('/home',express.static(path.join(__dirname,'/Public')));

//app.use('/static', express.static(__dirname + 'index.html'));

io.on('connection', function (socket) {
  socket.on('message', function (data) {
  console.log(data)
  socket.emit('news', { hello: 'world' });
   });
    socket.on('another-message', function (data) {
    socket.emit('not-news', { hello: 'world' });
  });
});



http.listen(3000, function(){
  console.log('listening on *:3000');
});

my index.html

<html>
<h1>working</h1>
<script src="/socket.io/socket.io.js"></script>
<script src ="script.js"></script>

<body>
    <ul id="messages"></ul>
    <form id ="target" action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

my script file

  var socket = io.connect('http://localhost:3000',{path:'/home/socket.io});
    socket.on('connect',function(){
    socket.emit('message', 'Hello server');
  });
   socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });

When i run "http://localhost:3000/home/" i get the error

Request URL:http://localhost:3000/home/socket.io/?EIO=3&transport=polling&t=LAN0qOj Request Method:GET Status Code:404 Not Found Remote Address:[::1]:3000

Please correct me.

  • 1
    see this answer http://stackoverflow.com/questions/29511404/connect-to-socket-io-server-with-specific-path-and-namespace – doron aviguy Jan 31 '16 at 09:59
  • @doron aviguy i did something like wat was mentioned there i have in my index.js "var io = require('socket.io')(http, { path: '/myapp/socket.io'});" but now i get the errors GET http://localhost:3000/socket.io/socket.io.js (index):6 Uncaught ReferenceError: io is not defined(anonymous function) @ (index):6 now the import of socket.io fails and it says io is not defined – Suresh Krishna Jan 31 '16 at 10:06
  • `require('socket.io')(http, { path: ''/myapp/socket.io"})` – doron aviguy Jan 31 '16 at 10:09

1 Answers1

0

Try it like this?

var socketio = require('soket.io');
var server = http.createServer(app).listen(3000);
var io = socketio.listen(server);
Nijeesh
  • 848
  • 7
  • 11
  • still getting same error "GET http://localhost:3000/home/socket.io/?EIO=3&transport=polling&t=LANBb-h 404 (Not Found)" – Suresh Krishna Jan 31 '16 at 10:17
  • http://stackoverflow.com/questions/27393705/socketio-get-http-localhost3000-socket-io-eio-3transport-pollingt-1418187 – Nijeesh Jan 31 '16 at 10:31