I'm new to socket session but I learned how it will works in wamp/xampp localhost. But when I move to server that is hosting. it will not work.
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
Above this work in wamp/xampp. but not found in hosting server. What should I give on src. My hosting is be like: aaa.bbb.com and its port is 8803 or bbb.com and its port is 8803.
I have tried the ways are to be include as like
<script src="/socket.io/socket.io.js"></script>
<script src="http://aaa.bbb.com:3000/socket.io/socket.io.js"></script>
<script src="http://bbb.com:3000/socket.io/socket.io.js"></script>
My server side code is
var express = require('express');
var app = express();
var socket = require('socket.io');
var server = require('http').createServer(app);
server.listen(3000);
var io = socket.listen(server);
var async = require('async');
var mysql= require('mysql');
var pool = mysql.createPool({
host : 'XXXXX',
user : 'XXXXX',
password : 'XXXX',
database:'XXXXX',
});
var chatserver=require('./chatserver.js');
var chatpage=io.of('/as/chatRoom').authorization(function (handshakeData, callback) {
console.dir(handshakeData);
handshakeData.page = '/welcome/chatRoom';
callback(null, true);
}).on('connection', function (socket) {
console.dir(socket.handshake.page);
chatserver.getUserFeeds(chatpage,socket,io,pool,async);
});
On the server side of index.js of node you will need to require io, on http server
var
app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http);`
I am unsing handlebars as my templating system so, o i go on that. On your home template file you will add
<script>
var socket = io.connect('http://bbb.com:3000');
socket.on('connect', function(){
socket.emit('authenticate', {data: "token"});
socket.on('error', function(err){ alert(err);
});
socket.on('unauthorized', function(err){
alert("Disconnected");
console.log("There was an error with the authentication:", err.message);
});
socket.on('disconnected', function() { alert('Disconnected') });
});
</script>
That works for me on live server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With