2016-03-13 05:19:24 +08:00
|
|
|
///<reference path="../../../typings/tsd.d.ts"/>
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|