Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node Socket.io links how will give in server not localhost(wamp/xampp)

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);
});
like image 848
Gopalakrishnan Avatar asked Aug 30 '16 12:08

Gopalakrishnan


1 Answers

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.

like image 68
DecoderNT Avatar answered Sep 20 '22 14:09

DecoderNT