node.js - socket.emit is not working in session.socket.io -


i trying create simple chat application using session.socket.io , rabbitmq , redis session storage.

app.js

var express = require('express'),         http = require('http'),         path = require('path'),         redis = require('redis'),         amqp = require('amqp'),         logger = require('morgan');     users = {};       var session = require('express-session');     var rabbitconn = amqp.createconnection({});     var chatexchange;     rabbitconn.on('ready', function () {         chatexchange = rabbitconn.exchange('chatexchange', { 'type': 'fanout' });     });      var app = express();     var server = http.createserver(app);      var io = require('socket.io')(server);      io.set("transports", ["polling"]);        var redisstore = require('connect-redis')(session),         rclient = redis.createclient(),         sessionstore = new redisstore({ client: rclient });       var cookieparser = require('cookie-parser');     var bodyparser = require('body-parser');     var methodoverride = require('method-override');       app.use(logger('dev'));     app.use(bodyparser.json());     app.use(bodyparser.urlencoded({ extended: true }));     app.use(methodoverride());       app.use(cookieparser());     app.use(session({ store: sessionstore, key: 'jsessionid', secret: 'your secret here', resave: true, saveuninitialized: true }));       var sessionsockets = require('session.socket.io');     var sessionsockets = new sessionsockets(io, sessionstore, cookieparser, 'jsessionid');      app.get('/', function (req, res) {         res.sendfile(__dirname + '/index.html');     });      sessionsockets.on('connection', function (err, socket, session) {         console.log('sessionsockets connected');         socket.on('send message', function (data) {             io.sockets.emit('new message', { message: data, user: socket.nickname });         }).on('new user', function (data, callback) {             console.log('new user called');             if (data in users) {                 callback(false);             } else {                 socket.nickname = data;                 users[socket.nickname] = socket;                 updateusername();                 callback(true);             }          }).on('disconnect', function () {             if (!socket.nickname) return;              delete users[socket.nickname];             updateusername();         }).on('connectiontest', function (data) {             console.log(data);         });          function updateusername() {             var test = {}             io.sockets.emit('usernames', object.keys(users));         }     });      server.listen(3003); 

index.html

<html>     <head>    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">     </head     <body>         <div id="nickwrap">             <p>enter username</p>             <p id="nickerror"></p>              <form id="setnick">                 <input size="35" id="nickname" />                 <input type="submit" />             </form>         </div>         <div id="contentwrap">             <div id="chatwrap">                 <div id="chat"></div>                 <form id="send-message">                     <input size="35" id="message" />                     <input type="submit" />                 </form>             </div>         </div>         <div id="users"></div>           <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>          <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>         <script src="/socket.io/socket.io.js"></script>         <script>             jquery(function($){                   var host = window.location.host;             console.log('host',host);             var socket = io.connect('http://' + host, {reconnect:false, 'try multiple transports':false});             var intervalid;             var reconnectcount = 0;              socket.on('connect', function () {                  socket.emit('connectiontest', 'hello server');                 console.log('connected');             });             socket.on('connecting', function () {                 console.log('connecting');             });             socket.on('disconnect', function () {                 console.log('disconnect');                 intervalid = setinterval(tryreconnect, 4000);             });             socket.on('connect_failed', function () {                 console.log('connect_failed');             });             socket.on('error', function (err) {                 console.log('error: ' + err);             });             socket.on('reconnect_failed', function () {                 console.log('reconnect_failed');             });             socket.on('reconnect', function () {                 console.log('reconnected ');             });             socket.on('reconnecting', function () {                 console.log('reconnecting');             });              var tryreconnect = function () {                 ++reconnectcount;                 if (reconnectcount == 5) {                     clearinterval(intervalid);                 }                 console.log('making dummy http call set jsessionid (before socket.io reconnect)');                 $.ajax('/')                     .success(function () {                         console.log("http request succeeded");                         //reconnect socket after got jsessionid set                         socket.socket.reconnect();                         clearinterval(intervalid);                     }).error(function (err) {                         console.log("http request failed (probably server not yet)");                     });             };                       var messageform=$("#send-message");                     var message=$("#message");                     var chat=$("#chat");                      var nickform=$("#setnick");                     var nickbox=$("#nickname");                     var nickerror=$("#nickerror");                      nickform.submit(function(e){                          e.preventdefault(e);                         socket.emit('new user',nickbox.val(),function(data){                              alert('hi');                             if(data){                                 $("#nickwrap").hide();                                 $("#contentwrap").show();                             }else{                                 nickerror.html('user name taken');                             }                         });                     });                      messageform.submit(function(e){                         e.preventdefault();                         socket.emit('send message',message.val());                         message.val('');                     });                      socket.on('new message',function(data){                         chat.append(data.message+':->'+data.user+'</br>');                     }).on('usernames',function(data){                     console.log(data);                         var html="";                         for(var i=0;i<data.length;i++){                             html+=data[i]+'</br>';                         }                         $("#users").html(html);                     });                  });         </script>     </body>      </html> 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -