node.js - Serve/Stop serve Socket.IO -


im wondering how can serve socket.io logged users?

for im adding/removing

<script src="/socket.io/socket.io.js"></script> <script>     var socket = io(); </script> 

but when remove after successful session, page not loading. idea using/serving socket.io users passport session authentication?

the true answer here use called handshake of socketio framework. allows checks , decide if should allow user connect server or not. other answers around don't automatically allow user connect. if opens console , instantiates socket against server - he's online.

check out: http://socket.io/docs/server-api/#namespace#use(fn:function):namespace

on each connection attempt, can run specific function see if things okay or not. can decline connection (calling next parameter), or accept - call next.

and that's :)

but here comes tricky part - how authenticate user? each socket instantiated simple http request client. it's later on upgraded socket connection.

if using kind of database or session, can use 1 of many modules out there. i've been using passport, happens automatically. here's more info how it: https://github.com/jfromaniello/passport.socketio

var io           = require("socket.io")(server), sessionstore     = require('awesomesessionstore'), // find working session store (have @ readme) passportsocketio = require("passport.socketio");  io.use(passportsocketio.authorize({     cookieparser: cookieparser,       // same middleware registrer in express     key:          'express.sid',       // name of cookie express/connect stores session_id     secret:       'session_secret',    // session_secret parse cookie     store:        sessionstore,        // need use sessionstore. no memorystore please     success:      onauthorizesuccess,  // *optional* callback on success - read more below     fail:         onauthorizefail,     // *optional* callback on fail/error - read more below }));  function onauthorizesuccess(data, accept){     console.log('successful connection socket.io');      // accept-callback still allows decide whether     // accept connection or not.     accept(null, true);      // or      // if use socket.io@1.x callback looks different     accept(); }  function onauthorizefail(data, message, error, accept){     if(error)         throw new error(message);     console.log('failed connection socket.io:', message);      // use callback log of our failed connections.     accept(null, false);      // or      // if use socket.io@1.x callback looks different     // if don't want accept connection     if(error)         accept(new error(message));     // error sent user special error-package     // see: http://socket.io/docs/client-api/#socket > error-object } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -