lib/socket.js
function init(trans) {
            // Assign `trans` to `transport` which is associated with the
            // socket.
            transport = trans;
            // When the transport has received a message from the server.
            transport.on("text", function(text) {
                // Converts JSON text to an event object.
                // 
                // It should have the following properties:
                // * `id: string`: an event identifier.
                // * `type: string`: an event type.
                // * `data: any`: an event data.
                // * `reply: boolean`: true if this event requires the reply.
                var event = JSON.parse(text);
                // If the server sends a plain event, dispatch it.
                if (!event.reply) {
                    self.emit(event.type, event.data);
                } else {
                    var latch;
                    // A function to create a function.
                    function reply(success) {
                        // A controller function.
                        return function(value) {
                            // The latch prevents double reply.
                            if (!latch) {
                                latch = true;
                                self.send("reply", {id: event.id, data: value, exception: !success});
                            }
                        };
                    }
                    // Here, the controller is passed to the handler as 2nd
                    // argument and calls the server's `resolved` or `rejected`
                    // callback by sending `reply` event.
                    self.emit(event.type, event.data, {resolve: reply(true), reject: reply(false)});
                }
            });
            // When any error has occurred.
            transport.on("error", function(error) {
                self.emit("error", error);
            });
            // When the transport has been closed for any reason.
            transport.on("close", function() {
                self.emit("close");
            });
        }
function init(trans) {
            // Assign `trans` to `transport` which is associated with the
            // socket.
            transport = trans;
            // When the transport has received a message from the server.
            transport.on("text", function(text) {
                // Converts JSON text to an event object.
                // 
                // It should have the following properties:
                // * `id: string`: an event identifier.
                // * `type: string`: an event type.
                // * `data: any`: an event data.
                // * `reply: boolean`: true if this event requires the reply.
                var event = JSON.parse(text);
                // If the server sends a plain event, dispatch it.
                if (!event.reply) {
                    self.emit(event.type, event.data);
                } else {
                    var latch;
                    // A function to create a function.
                    function reply(success) {
                        // A controller function.
                        return function(value) {
                            // The latch prevents double reply.
                            if (!latch) {
                                latch = true;
                                self.send("reply", {id: event.id, data: value, exception: !success});
                            }
                        };
                    }
                    // Here, the controller is passed to the handler as 2nd
                    // argument and calls the server's `resolved` or `rejected`
                    // callback by sending `reply` event.
                    self.emit(event.type, event.data, {resolve: reply(true), reject: reply(false)});
                }
            });
            // When any error has occurred.
            transport.on("error", function(error) {
                self.emit("error", error);
            });
            // When the transport has been closed for any reason.
            transport.on("close", function() {
                self.emit("close");
            });
        }
function init ( trans ) { // Assign `trans` to `transport` which is associated with the // socket. transport = trans ; // When the transport has received a message from the server. transport . on ( "text" , function ( text ) { // Converts JSON text to an event object. //  // It should have the following properties: // * `id: string`: an event identifier. // * `type: string`: an event type. // * `data: any`: an event data. // * `reply: boolean`: true if this event requires the reply. var event = JSON . parse ( text ) ; // If the server sends a plain event, dispatch it. if ( ! event . reply ) { self . emit ( event . type , event . data ) ; } else { var latch ; // A function to create a function. function reply ( success ) { // A controller function. return function ( value ) { // The latch prevents double reply. if ( ! latch ) { latch = true ; self . send ( "reply" , { id : event . id , data : value , exception : ! success } ) ; } } ; } // Here, the controller is passed to the handler as 2nd // argument and calls the server's `resolved` or `rejected` // callback by sending `reply` event. self . emit ( event . type , event . data , { resolve : reply ( true ) , reject : reply ( false ) } ) ; } } ) ; // When any error has occurred. transport . on ( "error" , function ( error ) { self . emit ( "error" , error ) ; } ) ; // When the transport has been closed for any reason. transport . on ( "close" , function ( ) { self . emit ( "close" ) ; } ) ; }
Now that working transport is determined, associates it with the socket.
Now that working transport is determined associates it with the socket .