-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.qml
96 lines (71 loc) · 3.1 KB
/
Server.qml
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
import QtQuick 2.5
import QtWebSockets 1.0
WebSocketServer {
id: server
onClientConnected: {
webSocket.onTextMessageReceived.connect(function(message) {
var json = JSON.parse(message)
// Normal message
if(json.type === "plain") {
// Check valid data
if(validateRecievedMessage(webSocket, json)) {
// Message is valid
sendTextMessage(webSocket, json)
}
}
// Image message
if(json.type === "image") {
// Check valid data
if(validateRecievedMessage(webSocket, json)) {
// Message is valid
sendImageMessage(webSocket, json)
}
}
// Register message
if(json.type === "register") {
// First message after socket connection
// Note that we can't validate register message because user has not any user_key yet
// If name is duplicate don't add new user
var user_key = newClient(webSocket, json)
if(user_key !== false) {
// Name is valid and is not taken yet
console.warn("New client connected:", user_key)
// Send accept message
sendAcceptMessage(user_key)
sendInfoMessage(qsTr("New user %1 now joined to chat").arg(json.name))
webSocket.onStatusChanged.connect(function() {
console.warn("Client status changed:", user_key, "Status:", webSocket.status)
if (webSocket.status === WebSocket.Error) {
// Only show errors to server
appendInfoMessage(qsTr("Error: %1 ").arg(webSocket.errorString));
} else if (webSocket.status === WebSocket.Closed) {
if(isValidUserKey(user_key)) {
var name = getUserName(user_key)
if(removeClient(user_key)) {
sendInfoMessage(qsTr("%1 left the chat").arg(name));
}
}
}
});
} else {
// Name is duplicate or invalid
if(isUserNameDuplicate(json.name)) {
// Name is duplicate
sendSingleErrorMessage(webSocket, qsTr("Name is duplicate"))
} else {
// Name is not duplicate but Invalid
sendSingleErrorMessage(webSocket, qsTr("Name is invalid"))
}
// Close web socket due to error
webSocket.active = false
}
}
// End register type message
});
}
onErrorStringChanged: {
if(errorString !== "") {
appendInfoMessage(qsTr("Server error: %1").arg(errorString))
}
}
}