ほとんど問題なく動作するサーバー送信イベント (SSE)の実装があります。私が抱えている唯一の問題は、「1 人のユーザーがサーバーに多くの接続を持つことができる」ことです。基本的に、ユーザーが複数の Web ブラウザーのタブを開くと、各タブはサーバーに新しいサーバー送信イベント要求を作成し、1 人のユーザーから多くの要求が実行されます。
この問題を解決するには、Javascript のSharedWorker内で SSE を実行したいと考えています。
これは、SharedWorker と通信する SSE が 1 つしかないことを意味します。次に、すべてのページ/Web ブラウザーが SharedWorker と通信します。これにより、ユーザーごとに 1 つの SSE のみを許可するという利点が得られます。
これは、現在、どのタイプのワーカーも使用せずに私の SSE がどのように機能しているかです。
$(function(){
//connect to the server to read messages
$(window).load(function(){
startPolling( new EventSource("poll.php") );
});
//function to listen for new messages from the server
function startPolling(evtSource){
evtSource.addEventListener("getMessagingQueue", function(e) {
var data = JSON.parse(e.data);
//handle recieved messages
processServerData(data);
}, false);
evtSource.onerror = function(e) {
evtSource.close();
};
}
});
同じセットアップを実行したいと思います。ただし、JavaScript の SharedWorker 内で実行して、ユーザーごとに複数の SSE を排除したいと考えています。
SharedWorker の実装に苦労しています。これが私がこれまでに試したことです
というファイルを作成し、worker.js
このコードを追加しました
var ports = [] ;
onconnect = function(event) {
var port = event.ports[0];
ports.push(port);
port.start();
var serv = new EventSource(icwsPollingUrl)
serv.addEventListener("getMessagingQueue", function(e) {
var data = JSON.parse(e.data);
processServerData(data);
}, false);
}
次に、メッセージにリストしたいページにこのコードがあります
$(function(){
$(window).load(function(){
var worker = new SharedWorker("worker.js");
worker.port.start();
worker.port.onmessage = function(e) {
console.log(e.data);
console.log('Message received from worker');
}
});
});
ここで何が欠けていますか?
私は何を間違っていますか?
実装を修正するにはどうすればよいですか?
編集済み
以下の@Bergiからのコメントに基づいて、コネクタにメッセージを投稿していない実装の更新バージョンを次に示します。コードで何が起こっているのかを理解するために、コードにコメントを追加しました。
ランディング ページで、つまりindex.php
、このように SharedWorker に接続します
$(function($){
//establish connection to the shared worker
var worker = new SharedWorker("/add-ons/icws/js/worker1.js");
//listen for a message send from the worker
worker.port.addEventListener("message",
function(event) {
console.log(event.data);
}
, false
);
//start the connection to the shared worker
worker.port.start();
});
これは私のworker1.js
ファイルに含まれているコードです
var ports = [] ;
//runs only when a new connection starts
onconnect = function(event) {
var port = event.ports[0];
ports.push(port);
port.start();
//implement a channel for a communication between the connecter and the SharedWorker
port.addEventListener("message",
function(event) {
listenForMessage(event, port);
}
);
}
//reply to any message sent to the SharedWorker with the same message but add the phrase "SharedWorker Said: " to it
listenForMessage = function (event, port) {
port.postMessage("SharedWorker Said: " + event.data);
}
//runs every time and post the message to all the connected ports
function readNewMessages(){
var serv = new EventSource(icwsPollingUrl)
serv.addEventListener("getMessagingQueue", function(e) {
var queue = JSON.parse(e.data);
notifyAllPorts(queue);
}, false);
}
//check all open ports and post a message to each
function notifyAllPorts(msg){
for(i = 0; i < ports.length; i++) {
ports[i].postMessage(msg);
}
}
ここに私の別のバージョンがありますworker1.js
var ports = [] ;
//runs only when a new connection starts
onconnect = function(event) {
var port = event.ports[0];
ports.push(port);
port.start();
//implement a channel for a communication between the connecter and the SharedWorker
port.addEventListener("message",
function(event) {
listenForMessage(event, port);
}
);
}
//reply to any message sent to the SharedWorker with the same message but add the phrase "SharedWorker Said: " to it
listenForMessage = function (event, port) {
port.postMessage("SharedWorker Said: " + event.data);
}
readNewMessages();
//runs every time and post the message to all the connected ports
function readNewMessages(){
console.log('Start Reading...');
var serv = new EventSource(icwsPollingUrl);
serv.addEventListener("getMessagingQueue", function(e) {
var queue = JSON.parse(e.data);
console.log('Message Received');
console.log(queue);
notifyAllPorts(queue);
}, false);
}
//check all open ports and post a message to each
function notifyAllPorts(msg){
for(i = 0; i < ports.length; i++) {
ports[i].postMessage(msg);
}
}