forked from Apoxx/spawnkill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketMessage.js
More file actions
62 lines (50 loc) · 1.26 KB
/
SocketMessage.js
File metadata and controls
62 lines (50 loc) · 1.26 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
"use strict";
/**
* Représente un message envoyé ou reçu via une connexion websocket
*/
SK.SocketMessage = function(id, data) {
this.id = id;
this.data = data;
};
/**
* {string} id Identifiant du message.
*/
SK.SocketMessage.prototype.id = "";
/**
* {mix} data Données du message
*/
SK.SocketMessage.prototype.data = null;
SK.SocketMessage.prototype.toJson = function() {
return JSON.stringify({
id: this.id,
data: this.data
});
};
/**
* Crée un Message à partir d'une chaîne JSON.
* @param {string} json Du type { id: "identifiant", data: "données" }
*/
SK.SocketMessage.fromJson = function(json) {
var messageObject = null;
try {
messageObject = JSON.parse(json);
}
catch (e) {
return false;
}
if(messageObject === null ||
typeof messageObject !== "object" ||
typeof messageObject.id !== "string"
) {
return false;
}
var id = messageObject.id;
var data = typeof messageObject.data !== "undefined" ? messageObject.data : null;
var message = new SK.SocketMessage(id, data);
return message;
};
SK.SocketMessage.fromData = function(id, data) {
data = data || null;
var message = new SK.SocketMessage(id, data);
return message;
};