本文整理汇总了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();
}
});
};
示例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: 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.');
示例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
示例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);
}