本文整理匯總了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);
}