本文整理汇总了TypeScript中websocket.server.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript server.on方法的具体用法?TypeScript server.on怎么用?TypeScript server.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类websocket.server
的用法示例。
在下文中一共展示了server.on方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
module.exports = (server: http.Server) => {
/**
* Init websocket server
*/
const ws = new websocket.server({
httpServer: server
});
ws.on('request', async (request) => {
const connection = request.accept();
if (request.resourceURL.pathname === '/server-stats') {
serverStatsStream(request, connection);
return;
}
if (request.resourceURL.pathname === '/notes-stats') {
notesStatsStream(request, connection);
return;
}
const ev = new Xev();
connection.once('close', () => {
ev.removeAllListeners();
});
const q = request.resourceURL.query as ParsedUrlQuery;
const [user, app] = await authenticate(q.i as string);
if (request.resourceURL.pathname === '/games/reversi-game') {
reversiGameStream(request, connection, ev, user);
return;
}
if (user == null) {
connection.send('authentication-failed');
connection.close();
return;
}
const channel: any =
request.resourceURL.pathname === '/' ? homeStream :
request.resourceURL.pathname === '/local-timeline' ? localTimelineStream :
request.resourceURL.pathname === '/hybrid-timeline' ? hybridTimelineStream :
request.resourceURL.pathname === '/global-timeline' ? globalTimelineStream :
request.resourceURL.pathname === '/user-list' ? userListStream :
request.resourceURL.pathname === '/drive' ? driveStream :
request.resourceURL.pathname === '/messaging' ? messagingStream :
request.resourceURL.pathname === '/messaging-index' ? messagingIndexStream :
request.resourceURL.pathname === '/games/reversi' ? reversiStream :
null;
if (channel !== null) {
channel(request, connection, ev, user, app);
} else {
connection.close();
}
});
};
示例2: async
module.exports = (server: http.Server) => {
/**
* Init websocket server
*/
const ws = new websocket.server({
httpServer: server
});
ws.on('request', async (request) => {
const connection = request.accept();
const user = await authenticate(connection);
// Connect to Redis
const subscriber = redis.createClient(
config.redis.port, config.redis.host);
connection.on('close', () => {
subscriber.unsubscribe();
subscriber.quit();
});
const channel =
request.resourceURL.pathname === '/' ? homeStream :
request.resourceURL.pathname === '/messaging' ? messagingStream :
null;
if (channel !== null) {
channel(request, connection, subscriber, user);
} else {
connection.close();
}
});
};
示例3: run
run() {
this.app_.init(this);
this.diposable_ = new CompositeDisposable();
this.sessionManager_ = new SessionManager(() => {
this.app_.createSessionData() });
this.connections_ = [];
const koaApp = new Koa();
koaApp.use(this.sessionManager_.asKoaMiddleware());
koaApp.use(koaStatic("static", {}));
const port = 3003;
console.log(`start listen at ${port}`);
const httpServer = http.createServer(koaApp.callback());
const webSocketServer = new WebSocketServer({
httpServer: httpServer
});
webSocketServer.on("request", (request: WebSocketRequest) => {
let sid: string = null;
request.cookies.forEach((cookie) => {
if (cookie.name == "sid") {
sid = cookie.value;
return;
}
});
if (sid == null) {
request.reject(403, "no sid");
return;
}
let session = this.sessionManager_.openSession(sid);
if (session == null) {
request.reject(403, "invalid session id");
return;
}
const socket = new SocketImpl(request.accept(null, null));
this.createConnection(socket, sid);
});
httpServer.listen(port);
}
示例4: originIsAllowed
autoAcceptConnections: false
});
function originIsAllowed(origin) {
return true;
}
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.sendUTF(message.utf8Data);
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
示例5: function
//var clients = [];
// This callback function is called every time someone tries to connect to the WebSocket server.
wsServer.on('request', function (request) {
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
var wssi = new websocetserver();
wssi.connection = request.accept(null, request.origin);
// we need to know client index to remove them on 'close' event
var index = websocetserverclass.clients.push(wssi.connection) - 1;
// var prop = Object.getOwnPropertyNames(User);
//prop.forEach((v, i, a) => { console.log((new Date()) + ' User is known as: ' + v + " - "+ i + " - "+ a); });
console.log((new Date()) + ' Connection accepted.');
if (websocetserverclass.history.length > 0) { wssi.connection.sendUTF(JSON.stringify({ type: 'history', data: history })); }
wssi.connection.on('message', wssi.HandleMessage);
wssi.connection.on('close', function( c ){ wssi.close(c); websocetserverclass.clients.splice(index, 1);});
});