1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/model/network.service.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-03-14 20:20:29 +08:00
///<reference path="../../browser.d.ts"/>
2016-03-13 05:19:24 +08:00
import * as io from 'socket.io-client';
import {Injectable} from 'angular2/core';
import {OnInit} from "angular2/core";
2016-03-13 18:28:29 +08:00
import {LoginCredential} from "../../../common/entities/LoginCredential";
import {MessageTypes} from "../../../common/MessageTypes";
import {Event} from "../../../common/event/Event";
import {User} from "../../../common/entities/User";
import {Message} from "../../../common/entities/Message";
2016-03-13 05:19:24 +08:00
@Injectable()
2016-03-13 18:28:29 +08:00
export class NetworkService{
2016-03-13 05:19:24 +08:00
private _socketIO: SocketIOClient.Socket;
2016-03-13 18:28:29 +08:00
public OnAuthenticated:Event<User>;
constructor(){
2016-03-13 05:19:24 +08:00
this._socketIO = io();
2016-03-13 18:28:29 +08:00
this.OnAuthenticated = new Event();
this._subscribeMessages();
2016-03-13 05:19:24 +08:00
}
2016-03-13 18:28:29 +08:00
public login(credential:LoginCredential){
this._socketIO.emit(MessageTypes.Client.Login.Authenticate,credential);
}
private _subscribeMessages(){
this._socketIO.on(MessageTypes.Server.Login.Authenticated, (message:Message<User>) =>{
if(message.error){
//TODO: Handle error
}else{
this.OnAuthenticated.trigger(message.result);
}
});
2016-03-13 05:19:24 +08:00
}
}