aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-07-17 21:42:37 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-07-17 21:42:37 -0400
commit8f832e7ec46a07bc8d40699856c487c64a26702d (patch)
tree2167543b5b1784293ccc0d12f131e7cd611bd048
parent80d427c30d46937b9a2491ace764a9fc01318841 (diff)
Socket made into an ES6 class
-rw-r--r--examples/jarvis.js1
-rw-r--r--socket.js160
2 files changed, 82 insertions, 79 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index ac27cea..7551a0d 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -30,7 +30,6 @@ class MetaSocket extends Socket{
self.on('init',(send)=>{
self.sender = send;
});
- Search.call(this);
}
}
var main = new MetaSocket();
diff --git a/socket.js b/socket.js
index 47e558d..3d8e63b 100644
--- a/socket.js
+++ b/socket.js
@@ -7,99 +7,103 @@ let mod = (div, end) => ( ( (div % end) + end ) % end ) // Makes it such that (-
class retryws extends EventEmitter{ // a wrapper on ws that retries on failure
constructor(addr) {
super();
- let sock;
- let sockdown = true;
- this.send = (message) => {
- if (sockdown){
- throw "Socket is down!";
- }
- sock.send(message);
+ this._sock;
+ this._sockdown = true;
+ this._addr = addr;
+ this.sockinit();
+ }
+ send(message){
+ if (this._sockdown){
+ throw "Socket is down!";
}
+ this._sock.send(message);
+ }
+ sockinit(){ // addr is not expected to change, so not handed as input; similarly, sock is not passed in a chain because behavior is expected to be serial (either initializing or retrying)
let self = this;
- function sockinit(){ // addr is not expected to change, so not handed as input; similarly, sock is not passed in a chain because behavior is expected to be serial (either initializing or retrying)
- sock = new ws(addr);
- sock.on('open', () => { sockdown = false; self.emit('open'); });
- sock.on('close', (err) => { console.log('close', err); setTimeout(sockinit,1000); sockdown = true; self.emit('close'); });
- sock.on('error', (err) => { console.log('error', err); sock.close(); });
- sock.on('message', (message) => { self.emit('message',message);});
- }
- sockinit();
+ this._sock = new ws(this._addr);
+ this._sock.on('open', () => { self._sockdown = false; self.emit('open'); });
+ this._sock.on('close', (err) => { console.log('close', err);
+ setTimeout(self.sockinit,1000); self._sockdown = true; self.emit('close'); });
+ this._sock.on('error', (err) => { console.log('error', err); self._sock.close(); });
+ this._sock.on('message', (message) => { self.emit('message',message);});
}
+
}
class Socket extends retryws {
- constructor(world='') { // Takes the name of the world, which can be an empty string.
+ constructor(world=''){ // Takes the name of the world, which can be an empty string.
let loc = (world == '') ? '' : `${world}/`;
let addr = `wss://www.yourworldoftext.com/${loc}ws/`;
super(addr);
- let writect = 0; // A running counter to make the server write confirmations meaningful
- this.on('message', (msg)=>{
- msg = JSON.parse(msg);
- switch (msg.kind){
- case 'write':
- this.emit('write', msg.accepted, msg.rejected); break; // A confirmation message for writes (accepted/rejected updates)
- case 'cursor':
- this.emit('cursor', msg.positions.map(pos => [pos.tileY*8+pos.charY,pos.tileX*16+pos.charX]), msg.sender); break; // Must be used for self-identification
- case 'tileUpdate':
- this.emit('tileUpdate', msg.sender, msg.source, msg.tiles); break; // Any change by another user or possibly oneself
- case 'fetch':
- this.emit('fetch', msg.tiles); break; // The response to a fetch request
- }
- });
+ this._writect = 0; // A running counter to make the server write confirmations meaningful
+ this.on('message', this._msgparse);
+ }
+ _msgparse(msg){
+ msg = JSON.parse(msg);
+ switch (msg.kind){
+ case 'write':
+ this.emit('write', msg.accepted, msg.rejected); break; // A confirmation message for writes (accepted/rejected updates)
+ case 'cursor':
+ this.emit('cursor', msg.positions.map(pos => [pos.tileY*8+pos.charY,pos.tileX*16+pos.charX]), msg.sender); break; // Must be used for self-identification
+ case 'tileUpdate':
+ this.emit('tileUpdate', msg.sender, msg.source, msg.tiles); break; // Any change by another user or possibly oneself
+ case 'fetch':
+ this.emit('fetch', msg.tiles); break; // The response to a fetch request
+ }
+ }
- this.fetch = function(coords){ //coords is a list of min/max pairs of y/x coordinate pairs which each describe at most 1000 tiles
- for (let i=0; i<coords.length; i++){
- let c = coords[i];
- if ( (c[1][0]-c[0][0]+1) * (c[1][1]-c[0][1]+1) > 1000) throw "Fetch has overlarge rectangular request";
- coords[i] = {"minY":c[0][0], "minX":c[0][1], "maxY":c[1][0], "maxX":c[1][1]};
- }
- this.send( JSON.stringify({
- "fetchRectangles": coords,
- "kind": "fetch",
- "v": "3"
- }));
+ fetch(coords){ //coords is a list of min/max pairs of y/x coordinate pairs which each describe at most 1000 tiles
+ for (let i=0; i<coords.length; i++){
+ let c = coords[i];
+ if ( (c[1][0]-c[0][0]+1) * (c[1][1]-c[0][1]+1) > 1000) throw "Fetch has overlarge rectangular request";
+ coords[i] = {"minY":c[0][0], "minX":c[0][1], "maxY":c[1][0], "maxX":c[1][1]};
}
+ this.send( JSON.stringify({
+ "fetchRectangles": coords,
+ "kind": "fetch",
+ "v": "3"
+ }));
+ }
- this.write = function(chars){ //chars is an list of pairs [ pixel coordinate (y/x < 16), char ]
- if (chars.length > 200) throw "Too many characters to write by server decree";
- if (chars.length == 0) throw "Send at least a character.";
- let write = [];
- for (let cha of chars){
- let coord = cha[0];
- write.push([
- Math.floor(coord[0]/8),
- Math.floor(coord[1]/16),
- mod(coord[0], 8),
- mod(coord[1], 16),
- 0,
- cha[1],
- writect
- ]);
- writect++;
- }
- this.send( JSON.stringify({
- "edits": write,
- "kind": "write"
- }));
- return writect-1; // Last label within write for cross-referencing with confirmation
+ write(chars){ //chars is an list of pairs [ pixel coordinate (y/x < 16), char ]
+ if (chars.length > 200) throw "Too many characters to write by server decree";
+ if (chars.length == 0) throw "Send at least a character.";
+ let write = [];
+ for (let cha of chars){
+ let coord = cha[0];
+ write.push([
+ Math.floor(coord[0]/8),
+ Math.floor(coord[1]/16),
+ mod(coord[0], 8),
+ mod(coord[1], 16),
+ 0,
+ cha[1],
+ this._writect
+ ]);
+ this._writect++;
}
+ this.send( JSON.stringify({
+ "edits": write,
+ "kind": "write"
+ }));
+ return this._writect-1; // Last label within write for cross-referencing with confirmation
+ }
- this.cursor = function(coords){ //coords is just one pair of char coords; I think the api could handle more, but it's unnecessary for now.
- this.send( JSON.stringify({
- "kind": "cursor",
- "positions":
- [
- {
- "tileY": Math.floor(coords[0]/8),
- "tileX": Math.floor(coords[1]/16),
- "charY": coords[0] % 8,
- "charX": coords[1] % 16
- }
- ]
- }));
- }
-
+ cursor(coords){ //coords is just one pair of char coords; I think the api could handle more, but it's unnecessary for now.
+ this.send( JSON.stringify({
+ "kind": "cursor",
+ "positions":
+ [
+ {
+ "tileY": Math.floor(coords[0]/8),
+ "tileX": Math.floor(coords[1]/16),
+ "charY": coords[0] % 8,
+ "charX": coords[1] % 16
+ }
+ ]
+ }));
}
+
}
module.exports = Socket;