Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

inital boilerplate for sharing sockets to make this effecient #249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions loader.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
// ==/UserScript==

var s = document.createElement( 'script' );
s.src = 'https://raw.github.com/Zirak/SO-ChatBot/master/master.js';
document.head.appendChild( s );
s.src = 'http://jscripts.local/chatbot-master.js';

window.addEventListener('stackexchange-socket-saved', function () {
document.head.appendChild( s );
});
40 changes: 25 additions & 15 deletions master.js
Original file line number Diff line number Diff line change
Expand Up @@ -2673,22 +2673,32 @@ var polling = bot.adapter.in = {
},

openSocket : function ( url, discard ) {
//chat sends an l query string parameter. seems to be the same as the
// since xhr parameter, but I didn't know what that was either so...
//putting in 0 got the last shitload of messages, so what does a high
// number do? (spoiler: it "works")
var socket = new WebSocket( url + '?l=99999999999' );

if ( discard ) {
socket.onmessage = function () {
socket.close();
};
}
else {
this.socket = socket;
socket.onmessage = this.ondata.bind( this );
socket.onclose = this.socketFail.bind( this );
var socket;
// socket-saver extension specific code:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openSocket is called (eventually) from /join to, well, join a room. That's why there's the discard option...

So we either fix up /join to do something else or change openSocket or its caller to match the new behaviour.

if ( typeof window.__stackexchangeChatSavedSocket !== 'undefined' ) {
var potentialSocket = window.__stackexchangeChatSavedSocket;
console.assert( potentialSocket.constructor === window.WebSocket );
console.assert( potentialSocket.url.startsWith( 'wss://chat.sockets.stackexchange.com' ) );
socket = potentialSocket;
} else {
//chat sends an l query string parameter. seems to be the same as the
// since xhr parameter, but I didn't know what that was either so...
//putting in 0 got the last shitload of messages, so what does a high
// number do? (spoiler: it "works")
socket = new WebSocket( url + '?l=99999999999' );

if ( discard ) {
socket.onmessage = function () {
socket.close();
};
return;
}
}
this.socket = socket;
socket.addEventListener( 'message', this.ondata.bind( this ) );
socket.addEventListener( 'close', this.socketFail.bind( this ) );
// socket.onmessage = this.ondata.bind( this );
// socket.onclose = this.socketFail.bind( this );
},

ondata : function ( messageEvent ) {
Expand Down
7 changes: 3 additions & 4 deletions master.min.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions socket-saver.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ==UserScript==
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a new userscript? Can't we conjoin the two quite easily? Instead of raising an event, inject a script.

We also have to think about the non-userscript scenario, the bookmarklet. From personal experience it's yet to fail me, but given that the userscript sometimes fails, maybe the bookmarklet sometimes fails too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the plan.

I will merge the two in a single script if you think it is working fine.
(in the original pr message :P)

I just want to have @rlemon test it and make sure it works -_-

// @name SO-Chat-Socket-Saver
// @namespace http://awal.js.org/
// @version 0.1
// @description i dunno what to put this is a template lmao
// @author awal
// @match *://chat.stackoverflow.com/rooms/*
// @match *://meta.chat.stackexchange.com/rooms/*
// @match *://chat.stackexchange.com/rooms/*
// @grant none
// @run-at document-start
// ==/UserScript==

if (document.body) {
console.warn('Socket Saver: Not properly running at document-start, setup shit properly FFS!');
} else {
// we are running at document-start properly. yipee!!
Object.defineProperty(WebSocket.prototype, 'onopen', {
set: function (val) {
if (this.url.startsWith('wss://chat.sockets.stackexchange.com')) {
console.info('Socket Saver: socket saved properly, use it with window.__stackexchange_chat_saved_socket');
window.__stackexchangeChatSavedSocket = this;
window.dispatchEvent(new CustomEvent('stackexchange-socket-saved'));
}
}
});
}
40 changes: 25 additions & 15 deletions source/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,32 @@ var polling = bot.adapter.in = {
},

openSocket : function ( url, discard ) {
//chat sends an l query string parameter. seems to be the same as the
// since xhr parameter, but I didn't know what that was either so...
//putting in 0 got the last shitload of messages, so what does a high
// number do? (spoiler: it "works")
var socket = new WebSocket( url + '?l=99999999999' );

if ( discard ) {
socket.onmessage = function () {
socket.close();
};
}
else {
this.socket = socket;
socket.onmessage = this.ondata.bind( this );
socket.onclose = this.socketFail.bind( this );
var socket;
// socket-saver extension specific code:
if ( typeof window.__stackexchangeChatSavedSocket !== 'undefined' ) {
var potentialSocket = window.__stackexchangeChatSavedSocket;
console.assert( potentialSocket.constructor === window.WebSocket );
console.assert( potentialSocket.url.startsWith( 'wss://chat.sockets.stackexchange.com' ) );
socket = potentialSocket;
} else {
//chat sends an l query string parameter. seems to be the same as the
// since xhr parameter, but I didn't know what that was either so...
//putting in 0 got the last shitload of messages, so what does a high
// number do? (spoiler: it "works")
socket = new WebSocket( url + '?l=99999999999' );

if ( discard ) {
socket.onmessage = function () {
socket.close();
};
return;
}
}
this.socket = socket;
socket.addEventListener( 'message', this.ondata.bind( this ) );
socket.addEventListener( 'close', this.socketFail.bind( this ) );
// socket.onmessage = this.ondata.bind( this );
// socket.onclose = this.socketFail.bind( this );
},

ondata : function ( messageEvent ) {
Expand Down