Created
April 29, 2017 20:02
-
-
Save Pompeu/f98864338614a8abc0c07bb4482fdd1d to your computer and use it in GitHub Desktop.
websocktes.js
This file contains hidden or 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
| (function () { | |
| 'use strict'; | |
| angular.module('app') | |
| .factory('socket', socket); | |
| socket.$inject =['$rootScope']; | |
| function socket ($rootScope) { | |
| /* | |
| ese io ai vem do socket.io.js | |
| */ | |
| let socket = io("url"); | |
| let service = { | |
| on : on, | |
| emit : emit | |
| }; | |
| return service; | |
| //essa função funciona como um listener entre cliente e o servidor | |
| function on (ev, cb) { | |
| socket.on(ev, function() { | |
| let msg = arguments; | |
| $rootScope.$apply(function () { | |
| cb.apply(socket, msg); | |
| }); | |
| }); | |
| } | |
| //essa função emite os eventos para servidor | |
| function emit (ev,data , cb ) { | |
| socket.emit(ev, data, msg => { | |
| $rootScope.$apply(() => { | |
| if(cb){ | |
| cb.apply(socket,msg); | |
| } | |
| }); | |
| }); | |
| } | |
| } | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment