aboutsummaryrefslogtreecommitdiff
path: root/utils/ident.js
diff options
context:
space:
mode:
Diffstat (limited to 'utils/ident.js')
-rw-r--r--utils/ident.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/utils/ident.js b/utils/ident.js
new file mode 100644
index 0000000..6d18b88
--- /dev/null
+++ b/utils/ident.js
@@ -0,0 +1,29 @@
+// A portable script to find the sender when the server websocket opens (by cursor movement)
+
+module.exports = function identify(sock, initOnce, init){
+ // `sock` should be Socket instance.
+ // initOnce and init should be functions that act like they describe; initOnce runs once per program execution and init whenever the socket turns back on
+
+ sock.on('open', ()=>{
+ // "Pings" the server with a cursor location which is detected and saved by identity()
+ let coords = [Math.floor(Math.random()*100000+16), Math.floor(Math.random()*1000000+16)];
+ sock.cursor(coords);
+ sock.on('cursor', detect);
+ function detect(pos, send){
+ if (vec.equals(pos[0],coords)){
+ sock.off('cursor', detect);
+ identity(send);
+ }
+ }
+ }
+
+ // Calls initOnce and init on successful identification.
+ let initialized = false;
+ function identity(send){
+ if (! initialized){
+ initialized = true;
+ initOnce();
+ }
+ init(send);
+ }
+}