Skip to content

Commit

Permalink
Merge pull request #272 from caijw/cjw-feat-websocket
Browse files Browse the repository at this point in the history
feat: pass websocket server as a function param to the handler
  • Loading branch information
huangyoukun authored Oct 16, 2018
2 parents 0c8c69a + c206dab commit 33ee7c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions bin/proxy/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function bind_listen(server) {
});
});
} else {
wsRoute.doRoute(ws, 'connection');
wsRoute.doRoute(ws, 'connection', { wsServer: server });
}

// 兼容以前的websocket逻辑,假的reponse事件,出现error方便打堆栈
Expand All @@ -212,7 +212,7 @@ function bind_listen(server) {
return;
}

wsRoute.doRoute(ws, 'message', message);
wsRoute.doRoute(ws, 'message', { message: message, wsServer: server });
reportWebSocketLog(ws);
});
ws.on('close', function(code, reason) {
Expand All @@ -221,7 +221,7 @@ function bind_listen(server) {
wsClient.close();
return;
}
wsRoute.doRoute(ws, 'close', code, reason);
wsRoute.doRoute(ws, 'close', { code: code, reason: reason, wsServer: server });
logger.debug('websocket server close, code : ${code}, reason : ${reason}', {
code,
reason
Expand Down Expand Up @@ -258,7 +258,7 @@ function bind_listen(server) {
wsClient.send('TSW_Websocket_proxy_server_error');
}
} else {
wsRoute.doRoute(ws, 'error', error);
wsRoute.doRoute(ws, 'error', { error: error, wsServer: server });
}
logger.debug('websocket server error : ${error}', {
error
Expand Down
10 changes: 5 additions & 5 deletions bin/proxy/ws.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports.getModAct = function (ws) {
return wsModAct.getModAct(ws);
};

exports.doRoute = function(ws, type, d1, d2) {
exports.doRoute = function(ws, type, opts) {
const wsModMap = require('./ws.mod.map');
const logger = require('logger');
const contextMod = require('context.js');
Expand Down Expand Up @@ -49,13 +49,13 @@ exports.doRoute = function(ws, type, d1, d2) {
};
}
if (type === 'connection') {
moduleObj.onConnection(ws);
moduleObj.onConnection(ws, opts.wsServer);
} else if (type === 'message') {
contextMod.currentContext().mod_act = mod_act;
moduleObj.onMessage(ws, d1);
moduleObj.onMessage(ws, opts.message, opts.wsServer);
} else if (type === 'close') {
moduleObj.onClose(ws, d1, d2);
moduleObj.onClose(ws, opts.code, opts.reason, opts.wsServer);
} else if (type === 'error') {
moduleObj.onError(ws, d1);
moduleObj.onError(ws, opts.error, opts.wsServer);
}
};

0 comments on commit 33ee7c7

Please sign in to comment.