• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript websocket.server类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中websocket.server的典型用法代码示例。如果您正苦于以下问题:TypeScript server类的具体用法?TypeScript server怎么用?TypeScript server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了server类的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: require

/// <reference path='./typings/tsd.d.ts' />
var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(3001, function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

var wsServer = new WebSocketServer({
    httpServer: server,
    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.');
开发者ID:bob12358,项目名称:codetank-backend,代码行数:31,代码来源:app.ts


示例4: require

import httpserver = require('./httpserver');



import websocetserverclass = require("./websocetserver");
var websocetserver = websocetserverclass.websocetserverinstance;

/** HTTP server */
http.createServer(httpserver.respond).listen(parseInt("8080", 10));

/** HTTP server 4 Websockets */
var server = http.createServer(function (request, response) {});
server.listen(webSocketsServerPort, function () { console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);});

/** WebSocket server  */
var wsServer = new webSocketServer({ httpServer: server });
//console.log((new Date()).toString() + collection);


//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
开发者ID:AndyN02,项目名称:WallServe,代码行数:31,代码来源:server.ts


示例5: CompositeDisposable

	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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript webtorrent.add函数代码示例发布时间:2022-05-25
下一篇:
TypeScript websocket.connection类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap