Mais conteúdo relacionado
Semelhante a 受託開発でのAngularJS - 第1回AngularJS 勉強会 at LIG (20)
Mais de Hayashi Yuichi (13)
受託開発でのAngularJS - 第1回AngularJS 勉強会 at LIG
- 32. 32
チャットアプリでAngularJS
Socket-Service
angular.module("services").factory("SocketService",function($rootScope){
if (typeof io === 'undefined') return;
var socket = io.connect('http://'+location.host + '/');
return {
on: function (type, listener) {
socket.on(type, function () {
var args = arguments;
$rootScope.$apply(function () {
listener.apply(socket, args);
});
});
},
emit: function (type, data, listener) {
socket.emit(type, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (listener) {
listener.apply(socket, args);
}
});
});
}
};
});
- 35. 35
チャットアプリでAngularJS
Node.js
var SocketIO = require('socket.io');
!
exports.init = function(server) {
var io = SocketIO.listen(server, {log:false});
io.sockets.on('connection', function(socket) {
socket.on('send',function(message) {
io.sockets.emit('receive',{
message: message,
date: new Date()
});
});
});
};