/** * Stalk-JavaScript, Node.js client. Supported react-native. * Support by@ nattapon.r@live.com * * Ahoo Studio.co.th */ import HttpStatusCode from './utils/httpStatusCode'; import TokenDecode from './utils/tokenDecode'; const Pomelo = require('../pomelo/webSocketClient'); import Config from '../../configs/config'; export interface IDictionary { [k: string]: string; } interface IAuthenData { userId: string; token: string; } class AuthenData implements IAuthenData { userId: string; token: string; } export interface IPomeloParam { host: string, port: number, reconnect: boolean } export default class ServerImplemented { private static Instance: ServerImplemented; public static getInstance(): ServerImplemented { if (this.Instance === null || this.Instance === undefined) { this.Instance = new ServerImplemented(); } return this.Instance; } static connectionProblemString: string = 'Server connection is unstable.'; pomelo: any; host: string; port: number | string; authenData: AuthenData; _isConnected = false; _isLogedin = false; connect = this.connectServer; constructor() { console.log("serv imp. constructor"); } public getClient() { let self = this; if (self.pomelo !== null) { return self.pomelo; } else { console.warn("disconnected."); } } public dispose() { console.warn("dispose socket client."); this.disConnect(); this.authenData = null; ServerImplemented.Instance = null; } public disConnect(callBack?: Function) { let self = this; if (!!self.pomelo) { self.pomelo.removeAllListeners(); self.pomelo.disconnect().then(() => { if (callBack) callBack(); }); } else { if (callBack) callBack(); } } public logout() { console.log('logout request'); let self = this; let registrationId = ""; let msg: IDictionary = {}; msg["username"] = this.username; msg["registrationId"] = registrationId; if (self.pomelo != null) self.pomelo.notify("connector.entryHandler.logout", msg); this.disConnect(); self.pomelo = null; } public init(callback: (err, res) => void) { console.log('serverImp.init()'); let self = this; this._isConnected = false; self.pomelo = Pomelo; self.host = Config.Stalk.chat; self.port = parseInt(Config.Stalk.port); if (!!self.pomelo) { // public UpdateUserProfile(myId: string, profileFields: { [k: string]: string }, callback: (err, res) => void) { let self = this; profileFields["token"] = this.authenData.token; profileFields["_id"] = myId; self.pomelo.request("auth.profileHandler.profileUpdate", profileFields, (result) => { if (callback != null) { callback(null, result); } }); } public ProfileImageChanged(userId: string, path: string, callback: (err, res) => void) { let self = this; var msg: { [k: string]: string } = {}; msg["token"] = this.authenData.token; msg["userId"] = userId; msg["path"] = path; self.pomelo.request("auth.profileHandler.profileImageChanged", msg, (result) => { if (callback != null) { callback(null, result); } }); } public getLastAccessRoomsInfo(msg: IDictionary, callback: Function) { let self = this; // /////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// /// Gets the public group chat rooms. /// Beware for data loading so mush. please load from cache before load from server. /// /// Callback. public getPrivateGroups(callback: (err, res) => void) { let self = this; var msg: IDictionary = {}; msg["token"] = this.authenData.token; self.pomelo.request("connector.entryHandler.getMyPrivateGroupChat", msg, (result) => { console.log("getPrivateGroups: " + JSON.stringify(result)); if (callback != null) { callback(null, result); } }); } public UserRequestCreateGroupChat(groupName: string, memberIds: string[], callback: (err, res) => void) { let self = this; var msg: IDictionary = {}; msg["token"] = this.authenData.token; msg["groupName"] = groupName; msg["memberIds"] = JSON.stringify(memberIds); self.pomelo.request("chat.chatRoomHandler.userCreateGroupChat", msg, (result) => { console.log("RequestCreateGroupChat", JSON.stringify(result)); if (callback != null) callback(null, result); }); } public UpdatedGroupImage(groupId: string, path: string, callback: (err, res) => void) { let self = this; var msg: IDictionary = {}; msg["token"] = this.authenData.token; msg["groupId"] = groupId; msg["path"] = path; self.pomelo.request("chat.chatRoomHandler.updateGroupImage", msg, (result) => { console.log("UpdatedGroupImage", JSON.stringify(result)); if (callback != null) { callback(null, result); } }); } public editGroupMembers(editType: string, roomId: string, roomType: RoomType, members: string[], callback: (err, res) => void) { let self = this; if (editType == null || editType.length === 0) return; if (roomId == null || roomId.length === 0) return; if (roomType === null) return; if (members == null || members.length === 0) return; var msg: IDictionary = {}; msg["token"] = this.authenData.token; msg["editType"] = editType; msg["roomId"] = roomId; msg["roomType"] = roomType.toString(); msg["members"] = JSON.stringify(members); self.pomelo.request("chat.chatRoomHandler.editGroupMembers", msg, (result) => { console.log("editGroupMembers response." + result.toString()); if (callback != null) { callback(null, result); } }); } public editGroupName(roomId: string, roomType: RoomType, newGroupName: string, callback: (err, res) => void) { let self = this; if (roomId == null || roomId.length === 0) return; if (roomType === null) return; if (newGroupName == null || newGroupName.length === 0) return; var msg: IDictionary = {}; msg["token"] = this.authenData.token; msg["roomId"] = roomId; msg["roomType"] = roomType.toString(); msg["newGroupName"] = newGroupName; self.pomelo.request("chat.chatRoomHandler.editGroupName", msg, (result) => { console.log("editGroupName response." + result.toString()); if (callback != null) { callback(null, result); } }); } /// /// Gets Private Chat Room. /// /// My identifier. /// My roommate identifier. public getPrivateChatRoomId(token: string, myId: string, myRoommateId: string, callback: (err, res) => void) { let self = this; var msg: IDictionary = {}; msg["token"] = token; msg["ownerId"] = myId; msg["roommateId"] = myRoommateId; self.pomelo.request("chat.chatRoomHandler.getRoomById", msg, (result) => { if (callback != null) { callback(null, result); } }); } //