-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsSource.js
More file actions
159 lines (147 loc) · 4.12 KB
/
JsSource.js
File metadata and controls
159 lines (147 loc) · 4.12 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function login_in(usrName_)
{
userInfo.usrName = usrName_
msgView.statusText = "You are logined as " + usrName_
}
function send(data)
{
var date = new Date()
if(userInfo.usrName == "")
{
login_in(data)
return
}
var msg = {
"usrName": "You(" + userInfo.usrName + ")",
"msgText": data,
"msgDate": date.toLocaleString(""),
}
var str_msg = JSON.stringify(msg)
history.addMessage(socket.ipAddr, str_msg)
msg = {
"usrName": userInfo.usrName,
"msgText": data,
"msgDate": date.toLocaleString(""),
}
str_msg = JSON.stringify(msg)
socket.sendData(str_msg)
if(socket.ipAddr == socket.localHost)
{
if(!rContainsIp(socket.ipAddr))
{
msgView.listModelR.append({
"usrName": "Local Storage",
"msgText": aToShort(data),
"ip": socket.ipAddr,
"chatId": last() === undefined ? chat + 1 : last().chatId + 1
})
}
changeChat(last() === undefined ? chat + 1 : last().chatId + 1, "127.0.0.1", "Local Storage")
}
else
if(!rContainsIp(socket.ipAddr))
{
msgView.listModelR.append({
"usrName": "Unkown",
"msgText": aToShort(data),
"ip": socket.ipAddr,
"chatId": last() === undefined ? chat + 1 : last().chatId + 1
})
}
add("You(" + userInfo.usrName + ")", data, date.toLocaleDateString(""))
}
function last()
{
return msgView.listModelR.get(msgView.listModelR.count - 1)
}
function add(usrName, msgText, msgDate)
{
if(usrName === userInfo.lastUser || usrName === "You(" + userInfo.usrName + ")")
{
var el = {
"usrName": usrName,
"msgText": msgText,
"msgDate": msgDate,
}
msgView.listModel.append(el)
}
for(var i = 0; i < msgView.listModelR.count; i++)
{
if(msgView.listModelR.get(i).ip === socket.ipAddr && msgView.listModelR.get(i).usrName !== "Local Storage")
{
if(usrName !== "You(" + userInfo.usrName + ")")
msgView.listModelR.get(i).usrName = usrName
msgView.listModelR.get(i).msgText = aToShort(msgText)
return
}
}
}
function aToShort(data)
{
var mt = "" + data
var atext = ""
if(mt.length > 14)
{
for(var j = 0; j < 8; j++)
{
atext += mt[j]
}
return atext + "..."
}
else
return mt
}
function rContains(chatId)
{
for(var j = 0; j < msgView.listModelR.count; j++)
{
if(msgView.listModelR.get(j).chatId === chatId)
return true
}
return false
}
function rContainsIp(ipAddr)
{
for(var j = 0; j < msgView.listModelR.count; j++)
{
if(msgView.listModelR.get(j).ip === ipAddr)
return true
}
return false
}
function changeChat(chatId, ip, usrName)
{
if(socket.ipAddr === ip)
return
chat = chatId
socket.ipAddr = ip
userInfo.lastUser = usrName
history.getMessages(ip)
}
function newMessage(usrName, msgText, msgDate,ipAddress)
{
history.addMessage(ipAddress, JSON.stringify(
{
"usrName": usrName,
"msgText": msgText,
"msgDate": msgDate
}))
if(userInfo.lastUser == "" || userInfo.lastUser == "Unkown")
{
if(last() !== undefined)
changeChat(last().chatId,
last().ip,
last().usrName)
userInfo.lastUser = usrName
add(usrName, msgText, msgDate)
for(var i = 0; i < msgView.listModelR.count; i++)
{
if(msgView.listModelR.get(i).chatId === chat)
{
msgView.listModelR.get(i).ip = ipAddress
}
}
}
else
add(usrName, msgText, msgDate)
}