mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
add connection log
This commit is contained in:
2 files changed
+15
-21
No files matched your search
@@ -13,7 +13,8 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
const secret = SystemModel.get('websocket.secret');
|
const secret = SystemModel.get('websocket.secret');
|
||||||
if (this.request.headers['x-hydro-websocket-gateway'] === secret) {
|
if (secret && this.request.headers['x-hydro-websocket-gateway'] === secret) {
|
||||||
|
this.ctx.logger('connection').info('Gateway websocket connection from %s', this.request.ip);
|
||||||
this.privileged = true;
|
this.privileged = true;
|
||||||
this.ctx.on('user/message', async (uid, mdoc) => {
|
this.ctx.on('user/message', async (uid, mdoc) => {
|
||||||
await this.notifyMessage(uid, mdoc);
|
await this.notifyMessage(uid, mdoc);
|
||||||
@@ -31,7 +32,7 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async accept(channel: string) {
|
accept(channel: string) {
|
||||||
if (this.channels.has(channel)) return;
|
if (this.channels.has(channel)) return;
|
||||||
this.channels.add(channel);
|
this.channels.add(channel);
|
||||||
if (this.privileged) return;
|
if (this.privileged) return;
|
||||||
@@ -45,8 +46,8 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async message(payload: any) {
|
async message(payload: any) {
|
||||||
const success = [];
|
const accept = [];
|
||||||
const failed = [];
|
const reject = [];
|
||||||
const session = payload?.credential
|
const session = payload?.credential
|
||||||
? await TokenModel.get(payload.credential, TokenModel.TYPE_SESSION)
|
? await TokenModel.get(payload.credential, TokenModel.TYPE_SESSION)
|
||||||
: null;
|
: null;
|
||||||
@@ -54,28 +55,19 @@ class WebsocketEventsConnectionManagerHandler extends ConnectionHandler {
|
|||||||
const user = await UserModel.getById('system', session.uid);
|
const user = await UserModel.getById('system', session.uid);
|
||||||
for (const channel of payload.channels) {
|
for (const channel of payload.channels) {
|
||||||
if (channel === 'message' && user.hasPriv(PRIV.PRIV_USER_PROFILE)) {
|
if (channel === 'message' && user.hasPriv(PRIV.PRIV_USER_PROFILE)) {
|
||||||
success.push(`message:${user._id}`);
|
accept.push(`message:${user._id}`);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
this.accept(`message:${user._id}`);
|
||||||
await this.accept(`message:${user._id}`);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
failed.push(channel);
|
reject.push(channel);
|
||||||
}
|
}
|
||||||
if (success.length) {
|
|
||||||
this.send({
|
this.send({
|
||||||
operation: 'accept',
|
operation: 'verify',
|
||||||
channels: success,
|
accept,
|
||||||
|
reject,
|
||||||
connection_id: payload.connection_id,
|
connection_id: payload.connection_id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (failed.length) {
|
|
||||||
this.send({
|
|
||||||
operation: 'reject',
|
|
||||||
channels: failed,
|
|
||||||
connection_id: payload.connection_id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function apply(ctx: Context) {
|
export function apply(ctx: Context) {
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ const messagePage = new AutoloadPage('messagePage', (pagename) => {
|
|||||||
const endpoint = url.toString().replace('http', 'ws');
|
const endpoint = url.toString().replace('http', 'ws');
|
||||||
if (window.SharedWorker) {
|
if (window.SharedWorker) {
|
||||||
try {
|
try {
|
||||||
initWorkerMode(url);
|
initWorkerMode(endpoint);
|
||||||
return;
|
return;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('SharedWorker init fail: ', e.message);
|
console.error('SharedWorker init fail: ', e.message);
|
||||||
@@ -137,7 +137,9 @@ const messagePage = new AutoloadPage('messagePage', (pagename) => {
|
|||||||
};
|
};
|
||||||
sock.onmessage = async (message) => {
|
sock.onmessage = async (message) => {
|
||||||
const payload = JSON.parse(message.data);
|
const payload = JSON.parse(message.data);
|
||||||
masterChannel.postMessage({ type: 'message', payload });
|
if (payload.operation === 'event') {
|
||||||
|
masterChannel.postMessage({ type: 'message', payload: payload.payload });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user