当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript server.on方法代码示例

本文整理汇总了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();
		}
	});
};
开发者ID:ha-dai,项目名称:Misskey,代码行数:60,代码来源:streaming.ts

示例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();
		}
	});
};
开发者ID:syuilo,项目名称:misskey-core,代码行数:34,代码来源:streaming.ts

示例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);
	}
开发者ID:omochi,项目名称:national-economy,代码行数:44,代码来源:Engine.ts

示例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.');
  });
});
开发者ID:bob12358,项目名称:codetank-backend,代码行数:30,代码来源:app.ts

示例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);});

});




开发者ID:AndyN02,项目名称:WallServe,代码行数:24,代码来源:server.ts


注:本文中的websocket.server.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。