UNPKG

740 BJavaScriptView Raw
1/*
2 * pub-sockets.js
3 * browserify entry point
4 * connect/disconnect socket.io
5 *
6 * TODO: consolidate with socket.io script
7 * copyright 2015-2020, Jürgen Leschner - github.com/jldec - MIT license
8*/
9
10/* global io */
11
12var debug = require('debug')('pub:sockets');
13
14if (window.io) {
15
16 debug('socket:connect');
17 var socket = io();
18
19 socket.on('reload', function() {
20 // in-browser generator case, just notify
21 if (window.generator && window.generator.emit) {
22 return window.generator.emit('notify', 'save');
23 }
24 debug('socket:reload');
25 location.reload();
26 });
27
28 window.addEventListener('beforeunload', function() {
29 debug('socket:disconnect');
30 socket.disconnect();
31 });
32
33} else console.log('no socket.io');