lib/transport-websocket-server.js
function createWebSocketTransport(ws) {
  // A transport object.
  var self = new events.EventEmitter();
  // Transport URI contains information like protocol header as query.
  self.uri = ws.upgradeReq.url;
  // Simply delegates WebSocket's events to transport and transport's behaviors to WebSocket.
  ws.onmessage = function(event) {
    // `event.data` is a message. It is string if text frame is sent and Buffer if binary frame
    // is sent.
    if (typeof event.data === "string") {
      self.emit("text", event.data);
    } else {
      self.emit("binary", event.data);
    }
  };
  ws.onerror = function(error) {
    self.emit("error", error);
  };
  // A flag to check the transport is opened.
  var opened = true;
  ws.onclose = function() {
    opened = false;
    self.emit("close");
  };
  self.send = function(data) {
    // Allows to send data only it is opened. If not, fires an error.
    if (opened) {
      // If `data` is string, a text frame is sent, and if it's Buffer, a binary frame is sent.
      ws.send(data);
    } else {
      self.emit("error", new Error("notopened"));
    }
    return this;
  };
  self.close = function() {
    ws.close();
    return this;
  };
  return self;
}
function createWebSocketTransport(ws) {
  // A transport object.
  var self = new events.EventEmitter();
  // Transport URI contains information like protocol header as query.
  self.uri = ws.upgradeReq.url;
  // Simply delegates WebSocket's events to transport and transport's behaviors to WebSocket.
  ws.onmessage = function(event) {
    // `event.data` is a message. It is string if text frame is sent and Buffer if binary frame
    // is sent.
    if (typeof event.data === "string") {
      self.emit("text", event.data);
    } else {
      self.emit("binary", event.data);
    }
  };
  ws.onerror = function(error) {
    self.emit("error", error);
  };
  // A flag to check the transport is opened.
  var opened = true;
  ws.onclose = function() {
    opened = false;
    self.emit("close");
  };
  self.send = function(data) {
    // Allows to send data only it is opened. If not, fires an error.
    if (opened) {
      // If `data` is string, a text frame is sent, and if it's Buffer, a binary frame is sent.
      ws.send(data);
    } else {
      self.emit("error", new Error("notopened"));
    }
    return this;
  };
  self.close = function() {
    ws.close();
    return this;
  };
  return self;
}
function createWebSocketTransport ( ws ) { // A transport object. var self = new events . EventEmitter ( ) ; // Transport URI contains information like protocol header as query. self . uri = ws . upgradeReq . url ; // Simply delegates WebSocket's events to transport and transport's behaviors to WebSocket. ws . onmessage = function ( event ) { // `event.data` is a message. It is string if text frame is sent and Buffer if binary frame // is sent. if ( typeof event . data === "string" ) { self . emit ( "text" , event . data ) ; } else { self . emit ( "binary" , event . data ) ; } } ; ws . onerror = function ( error ) { self . emit ( "error" , error ) ; } ; // A flag to check the transport is opened. var opened = true ; ws . onclose = function ( ) { opened = false ; self . emit ( "close" ) ; } ; self . send = function ( data ) { // Allows to send data only it is opened. If not, fires an error. if ( opened ) { // If `data` is string, a text frame is sent, and if it's Buffer, a binary frame is sent. ws . send ( data ) ; } else { self . emit ( "error" , new Error ( "notopened" ) ) ; } return this ; } ; self . close = function ( ) { ws . close ( ) ; return this ; } ; return self ; }
WebSocket is a protocol designed for a full-duplex communications over a TCP connection.
WebSocket is a protocol designed for a full - duplex communications over a TCP connection .