{"size": 2198, "ext": "js", "max_stars_repo_path": "examples/readme.js", "max_stars_repo_name": "newtoncodes/rtm", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "examples/readme.js", "max_issues_repo_name": "newtoncodes/rtm", "max_issues_repo_issues_event_min_datetime": "2017-08-15T16:47:56.000Z", "max_issues_repo_issues_event_max_datetime": "2017-11-06T10:39:18.000Z", "max_forks_repo_path": "examples/readme.js", "max_forks_repo_name": "newtoncodes/rtm", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 27.475, "max_line_length": 91, "alphanum_fraction": 0.5868971793}
'use strict';

const RTMSocket = require('../');

// First we extend for all sockets we will need.

class ServerSocket extends RTMSocket {
    constructor(socket) {
        super(socket);

        this._handlers = {
            getTime: (data, callback) => {
                console.log('Client invoked getTime.');
                callback(null, Date.now());
            }
        };
    }

    getClientTime() {
        this.invoke('getTime', null, (error, result) => {
            console.log('Client time result:', error, result);
        });
    }
}

class ClientSocket extends RTMSocket {
    constructor(socket) {
        super(socket);

        this._handlers = {
            getTime: (data, callback) => {
                console.log('Server invoked getTime.');
                callback(null, Date.now());
            }
        };
    }

    getServerTime() {
        this.invoke('getTime', null, (error, time) => {
            console.log('Server time result:', error, time);
        });
    }
}

// And now just wrap the client and the server sockets:

// Server code:

const io = require('socket.io');
let socket = io();
socket.listen(80);

socket.on('connection', (socket) => {
    console.log('Connection established.');

    socket = ServerSocket.wrap(socket);

    socket.on('testEvent', (foo, bar) => console.log('Client sends testEvent:', foo, bar));
    socket.once('disconnect_for_good', () => console.log('Connection closed.'));

    socket.getClientTime();
    client.ping((error, time) => console.log('Ping result:', error, time));
    socket.emitSafe('testEvent', 'foo', 'bar', error => console.log('Emitted: testEvent'));
});


// Client code:

const io2 = require('socket.io-client');
let client = ClientSocket.wrap(io2('http://localhost', {reconnectionAttempts: 2}));

client.on('connect', () => {
    client.on('testEvent', (foo, bar) => console.log('Server sends testEvent:', foo, bar));
    client.once('disconnect_for_good', () => console.log('Connection closed.'));

    client.getServerTime();
    client.ping((error, time) => console.log('Ping result:', error, time));
    client.emitSafe('testEvent', 'foo', 'bar', error => console.log('Emitted: testEvent'));
});
