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 all commits
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
18 changes: 17 additions & 1 deletion loader.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@
// @version 0.1
// @description Loads the bot when you visit a room
// @include /http://chat\.stack(overflow|exchange)\.com/rooms/\d+/
// @run-at document-start
// ==/UserScript==

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

if (document.body) {
console.warn('Socket Saver: Not properly running at document-start, setup shit properly FFS!');
document.head.appendChild( s );
} 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;
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' && !discard ) {
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.

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' && !discard ) {
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