如何在自定义原型方法中附加socket.io事件?

时间:2016-12-05 14:02:00

标签: javascript jquery node.js socket.io

如何在下一课中制作$.tpt.prototype.socket.on("connect", function() {});

skeleton.js:

var socket = io.connect('http://localhost:3003');
socket.on("connect", function () {
});


(function ($) {
  var uuid = {};  

  $.tpt = function (options) {
    this.boot();
  };

  $.tpt.prototype.boot = function(msg) {
    alert("page loaded");   
  };

  //attach that events of socket here how??

})(jQuery);

1 个答案:

答案 0 :(得分:1)

var socket = io.connect('http://localhost:3003');
socket.on("connect", function () {
});


(function ($) {
  var uuid = {};  

  $.tpt = function (options) {
    this.boot();
    this.socketOn('connect',function(){});

  };

  $.tpt.prototype.boot = function(msg) {
    alert("page loaded");   
  };

  $.tpt.prototype.socketOn = function(event,fn){
       socket.on(event,fn);
  };
})(jQuery);