Skip to content

WebSocket Server Improvments.

Compare
Choose a tag to compare
@mlaanderson mlaanderson released this 14 Dec 18:36
· 45 commits to master since this release

WebSocket handling was not standard in v1.x.x. In v2.0.0 the server only routes the UPGRADE request to the applet, and the applet handles the WebSocket server. If the applet has a session created using the setSession method, then the session will be added to upgrade request before the connection event is fired.

Example Applet with WebSocket Server:

class HelloApplet extends Applet {
    constructor(config) {
        super(config);

        process.nextTick((() => {
            // set the view engine and template location
            this.setViewEngine('ejs');
            this.setViewPath(path.join(__dirname, 'views'));

            // configure the static file directory
            this.setStaticContentPath(path.join(__dirname, 'public'));

            // Configure the session
            this.setSession('express-session', {
                secret: '95f0d5bd-b3aa-482d-8469-b6ee04776d8a',
                resave: false,
                saveUninitialized: true,
                cookie: { 
                    secure: true,
                    path: this.configuration.applet.container
                }
            });

            var wss = new WebSocket.Server( { server: this } );
            wss.on('connection', (socket, request) => {
                        socket.on('message', (data) => {
                                    console.log(request.session);
                                    console.log(data);
                        });
            });

            // Finally add the router 
            this.app.use('/', router);
        }).bind(this));
    }
}

module.exports = MyApplet;