Example #1
0
 constructor(private config: any) {
   //create logger
   this.logger = new Logger();
   this.logger.addListener(console.log);
   
   //Prepare dependency injection
   this.injector = new Injector();
   this.injector
     .bind('injector').to(this.injector)
     .bind('privateKeyPath').to(path.resolve(config.privateKeyPath ||
       __dirname + serverconfig.privateKeyFallback));
   this.injector.setResolver(this.dependencyResolver.bind(this));
   
   //create express server
   this.server = express();
   this.server.use(bodyParser.json());
   this.server.use(bodyParser.urlencoded({ extended: true }));
   this.httpServer = this.createServer();
   let webSocket = io(this.httpServer);
   webSocket.use(socketioJwt.authorize({
     secret: fs.readFileSync(this.injector.get('privateKeyPath'), 'utf8'),
     handshake: true
   }));
   
   //create DependencyManager
   let depManager = new DependencyManager();
   
   //link injected variables
   this.injector
     .bind('config').to(config)
     .bind('serverconfig').to(serverconfig)
     .bind('server').to(this.server)
     .bind('logger').to(this.logger)
     .bind('websocket').to(webSocket)
     .bind('depManager').to(depManager);
     
   //load extensions and modules
   this.loadExtensions();
   this.loadModules();
   
   //start Server
   this.httpServer.listen(this.config.server.port);
   this.logger.log('Server startet at port ' + this.config.server.port);
 }
			if (err) {
				rsp.writeHead(500);
				return rsp.end('Error loading index.html');
			}

			rsp.writeHead(200);
			rsp.end(data);
		});
});

const io = SocketIo(app);

// This example test code is using the Node Http Server

io.on('connection', authorize({
	secret: 'Your Secret Here',
	decodedPropertyName: 'anyNameYouWant'
}));

io.on('authenticated', (socket: SocketIo.Socket) => {
	console.log('authenticated!!');
	console.log(JSON.stringify((socket as any).anyNameYouWant));
});

const secrets: any = {
	user1: 'secret 1',
	user2: 'secret 2'
};

// Assume a claim name of userId
function secretFunc(request: any, payload: any, callback: JwtSecretFuncCallback): void {
	callback(null, secrets[payload.userId]);
Example #3
0
// parse application/json
app.use(bodyParser.json());

app.use(cors());

if (pathExists.sync("/app") && pathExists.sync("/index.html")) app.use("/", express.static(__dirname + "/app"));


const server = http.createServer(app);
const io = IO(server);

console.log("listen :" + options.port);


io.on('connection', socketioJwt.authorize({
    secret: options.secret,
    timeout: 15000 // 15 seconds to send the authentication message
})).on('authenticated', function (socket) {
    socket.join('inspects');
    console.log('new user')
    socket.on("subscribe", function (room) {
        console.log("joining room", room);
        socket.join(room);
    });
});

io.on("disconnection", function (socket) {
    // in socket.io 1.0
    console.log("bye! ");
});

let Docker = new docker();