Last active
January 1, 2016 12:19
-
-
Save kamiyam/8144209 to your computer and use it in GitHub Desktop.
socket.io mluti connecting room for sails.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do ( d=document, w=window, $=jQuery ) -> | |
# ref http://qiita.com/Layzie/items/465e715dae14e2f601de | |
_is = (type, obj) -> | |
clas = Object.prototype.toString.call(obj).slice(8, -1) | |
return obj isnt undefined && obj isnt null && clas is type | |
# log output | |
log = -> | |
if (typeof console isnt 'undefined') | |
console.log.apply(console, arguments); | |
class Socketer | |
constructor: ( @io ) -> | |
return | |
# connect | |
bind: ( room, connect ) -> | |
if ( _is( "Function", room ) ) | |
connect = room | |
room = "" | |
if ( ! _is( "String", room ) ) | |
log("error... first variable is need string") | |
return; | |
else if( room is "" ) | |
socket = @io.connect() | |
else | |
socket = @io.connect( room ) | |
socket.on "connect", -> | |
# callback | |
connect(socket) | |
socketer = w.socketer = new Socketer( w.io ) | |
socketer.bind( ( socket ) -> | |
# Listen for Comet messages from Sails | |
socket.on 'message', ( message ) -> | |
# /////////////////////////////////////////////////////////// | |
# // Replace the following with your own custom logic | |
# // to run when a new message arrives from the Sails.js | |
# // server. | |
# /////////////////////////////////////////////////////////// | |
log( 'New comet message received :: ', message ) | |
return | |
# ////////////////////////////////////////////////////// | |
# /////////////////////////////////////////////////////////// | |
# // Here's where you'll want to add any custom logic for | |
# // when the browser establishes its socket connection to | |
# // the Sails.js server. | |
# /////////////////////////////////////////////////////////// | |
log( | |
'Socket is now connected and globally accessible as `socket`.\n' + | |
'e.g. to send a GET request to Sails, try \n' + | |
'`socket.get("/", function (response) ' + | |
'{ console.log(response); })`' | |
) | |
return | |
# /////////////////////////////////////////////////////////// | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment