import { browserHistory } from 'react-router';

const instanceID = Math.floor(Math.random() * 10000) + 1;

export const eventTypes = {
    shownNotification: "SHOWN_NOTIFICATION",
    shownWarning: "SHOWN_WARNING",
    changedRoute: "CHANGED_ROUTE",
    appInitialised: "APP_INITIALISED",
    requestSent: "REQUEST_SENT",
    requestReceived: "REQUEST_RECEIVED"
}

browserHistory.listen(location => {
    log.add(eventTypes.changedRoute, {...location})
});

const socket = new WebSocket('ws://localhost:8081/florence/websocket');

socket.onopen = () => {
    log.add(eventTypes.appInitialised);
}

export default class log {

    static add(eventType, payload)  {
        const event = {
            type: eventType,
            location: location.href,
            instanceID,
            clientTimestamp: new Date().toISOString(),
            payload: payload || null
        }

        // console.log(event);

        // Socket isn't open yet but something has been loged, wait until it is open to send it
        if (socket.readyState === 0) {
            socket.onopen = () => {
                socket.send(`event:${JSON.stringify(event)}`);
            }
            return;
        }

        socket.send(`event:${JSON.stringify(event)}`);
        
        // Send across with a top level type because other data, not just events, will be sent too e.g.
        /*
        {
            type: "LOG_EVENT",
            payload: {
                event
            }
        }
        */
    }
}
import { browserHistory } from 'react-router';

const instanceID = Math.floor(Math.random() * 10000) + 1;

export const eventTypes = {
    shownNotification: "SHOWN_NOTIFICATION",
    shownWarning: "SHOWN_WARNING",
    changedRoute: "CHANGED_ROUTE",
    appInitialised: "APP_INITIALISED",
    requestSent: "REQUEST_SENT",
    requestReceived: "REQUEST_RECEIVED"
}

browserHistory.listen(location => {
    log.add(eventTypes.changedRoute, {...location})
});

const socket = new WebSocket(`ws://${window.location.host}/florence/websocket`);

socket.onopen = () => {
    log.add(eventTypes.appInitialised);
}

export default class log {

    static add(eventType, payload)  {
        const event = {
            type: eventType,
            location: location.href,
            instanceID,
            clientTimestamp: new Date().toISOString(),
            payload: payload || null
        }

        // console.log(event);

        // Socket isn't open yet but something has been loged, wait until it is open to send it
        if (socket.readyState === 0) {
            socket.onopen = () => {
                socket.send(`event:${JSON.stringify(event)}`);
            }
            return;
        }

        socket.send(`event:${JSON.stringify(event)}`);
        
        // Send across with a top level type because other data, not just events, will be sent too e.g.
        /*
        {
            type: "LOG_EVENT",
            payload: {
                event
            }
        }
        */
    }
}
Update websocket url to use dynamic host rather than fixed
Update websocket url to use dynamic host rather than fixed


Former-commit-id: f04b7d503048424669a9d139ce67b11c277435ad
Former-commit-id: 3e4a3612aa6a7173b17b02119663a1833b285469
Former-commit-id: 558d23de277ed4c459fc917f6574e97f528e0a7c