-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathchat.component.ts
94 lines (79 loc) · 3.25 KB
/
chat.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Component } from "@angular/core";
import * as globalVars from "../service/global";
/// <reference path="../../typings/globals/jquery/index.d.ts/>
import "/socket.io/socket.io.js";
@Component({
moduleId: module.id,
selector: "chat-page",
templateUrl: "./chat.component.html"
})
export class ChatComponent {
reference: any;
resFlag: boolean = false;
newUser: boolean = false;
exitedUser: boolean = false;
newUserName: string = null;
exitedUserName: string = null;
sentMessageUsername: string = null;
response: string;
clientsNameList: number[];
message: string;
msgCount: number = 0;
constructor() {
let reference = this;
let temp;
globalVars.socket.on("broadcastToAll_chatMessage", function(resObj) {
reference.msgCount++;
if (reference.sentMessageUsername !== resObj.name) {
resObj.name = resObj.name + ": ";
temp = $("#messages").length;
console.log("ul length : ", temp);
console.log(reference.msgCount);
$("#messages").append($("<li data-index=" + reference.msgCount + ">"));
$("li[data-index=" + reference.msgCount + "]").append($("<div class='left-msg' data-index=" + reference.msgCount + ">"));
$("div[data-index=" + reference.msgCount + "]").append($("<span class='name'>").text(resObj.name));
$("div[data-index=" + reference.msgCount + "]").append($("<span class='msg'>").text(resObj.msg));
$("#messages").append($("<br>"));
}
else if (reference.sentMessageUsername === resObj.name) {
$("#messages").append($("<li data-index=" + reference.msgCount + ">"));
$("li[data-index=" + reference.msgCount + "]").append($("<div class='right-msg' data-index=" + reference.msgCount + ">"));
$("div[data-index=" + reference.msgCount + "]").append($("<span class='msg'>").text(resObj.msg));
$("#messages").append($("<br>"));
reference.sentMessageUsername = null;
}
});
globalVars.socket.on("updateSocketList", function(list){
reference.clientsNameList = list;
});
globalVars.socket.on("addUserToSocketList", function(username){
reference.exitedUser = false;
reference.newUser = true;
reference.newUserName = username;
});
globalVars.socket.on("removeUserFromSocketList", function(username){
reference.newUser = false;
reference.exitedUser = true;
reference.exitedUserName = username;
});
}
sendMessage(data) {
this.resFlag = true;
let reference = this;
globalVars.socket.emit("chatMessageToSocketServer", data.value, function(respMsg, username){
reference.sentMessageUsername = username;
reference.response = respMsg;
});
$("#message-boxID").val(" ");
}
sendMessageOnEnter($event, messagebox) {
if ($event.which === 13) { // ENTER_KEY
this.sendMessage(messagebox);
}
}
update() {
this.resFlag = false;
this.newUser = false;
this.exitedUser = false;
}
}