summaryrefslogtreecommitdiff
path: root/scripts/socket/sock.js
blob: 4c64e0253fec4a8af05200d774ac70c21d7d351b (plain)
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
'use strict';

const VALID_EVENTS = ["error", "closed", "handshake", "menu", "game", "chat", "ping"];

// A wrapper around the wrapper 
class SockWorker extends EventTarget{

	constructor (serveraddr, version)
	{
		super();

		this.server = serveraddr;
		this.version = version;
		this.handshake = false;
	}

	// Initialize the connection.
	init () {
		this.handshake = false;
		if(this.server == "" || this.server == null) {
			return;
		}
		try {
			this.socket = new WebSocket(this.server);

			this.socket.addEventListener("open", this.o.bind(this));
			this.socket.addEventListener("message", this.msg.bind(this));

			this.socket.addEventListener("closed", this.c.bind(this));
			this.socket.addEventListener("error", this.err.bind(this));
		} catch (e) {
			this.err();
		}
	}

	// Called when the connection connects to the server
	o () {
		this.send("version", this.version);
	}

	// Called when the connection gets a message from the server
	// Attempts to turn the message into a usable object and pass it to the callback
	msg (e) {
		if(typeof e.data == "string") {
			let dat = JSON.parse(e.data);

			if (!this.handshake) {
				if (dat.type == "ready")
					this.handshake = true;
				this.dispatchEvent(new CustomEvent("handshake", {detail: dat.type}));
				return;
			}
			
			if (VALID_EVENTS.includes(dat.type))
				this.dispatchEvent(new CustomEvent(dat.type, {detail: dat.data}));
		}
	}

	// Called when the connection closes.
	// Passes a close object to the callback.
	c () {
		this.dispatchEvent(new Event("closed"));
	}

	// Called when the connection encounters an error.
	// Passes an error to the callback
	err () {
		this.dispatchEvent(new Event("error"));
	}

	// Call to close the connection to the server
	close () {
		this.socket.close();
	}

	// Send a message to the server
	send (type, data) {
		let m = new Message(type, data);
		this.socket.send(m.stringify())
	}

	// Raw message the server
	sendRaw (s) {
		this.socket.send(s)
	}
}