javascript - connecting socket to a specific path -
i have path localhost:3000/home want pass messages via socket.io. mu js file
`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 html file
<html> <h1>working</h1> <script src="/socket.io/socket.io.js"></script> <script> var iopath = ""; iopath = '/' + 'home'+ '/socket.io' var socket = io.connect('http://localhost:3000', { path : iopath}); //var socket = io.connect('http://localhost:3000'); socket.on('connect',function(){ socket.emit('message',{ 'msg' :'hello server'}); //socket.emit('message', 'hello server'); }); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script> <body> <ul id="messages"></ul> <form id ="target" action=""> <input id="m" autocomplete="off" /><button>send</button> </form> </body> </html>
i following error in browser console :"get http://localhost:3000/home/socket.io/?eio=3&transport=polling&t=la2fcjf 404 (not found)"
please correct me.
try
var io = require('socket.io')(http,{ path: '/home/socket.io'});
Comments
Post a Comment