Skip to content

Instantly share code, notes, and snippets.

@jedi4ever
Forked from joshhunt/index.js
Created December 6, 2015 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedi4ever/fbdb4938620293bfc5a0 to your computer and use it in GitHub Desktop.
Save jedi4ever/fbdb4938620293bfc5a0 to your computer and use it in GitHub Desktop.
Socket.io live reload for Apple TV TVML app
import 'babel-polyfill';
import firstView from 'views/first';
import * as liveReload from 'lib/liveReload';
App.onLaunch((launchOptions) => {
firstView();
liveReload.connect(launchOptions);
});
import io from 'socket.io-client';
import * as router from 'lib/router';
function resume({lastLocation}) {
if (!lastLocation) { return; }
router.goTo(lastLocation);
}
export function connect(launchOptions = {}) {
const socket = io(launchOptions.BASE_URL);
socket.on('connect', () => console.debug('Live reload: connected') );
socket.on('compile', () => console.debug('Live reload: compiling, prepare for reload') );
socket.on('live-reload', () => {
App.reload({when: 'now'}, {lastLocation: router.getLocation()});
});
if (launchOptions.reloadData) {
resume(launchOptions.reloadData || {});
}
}
io = require('socket.io')(server);
io.serveClient(false);
io.on('connection', (socket) => {
console.log('socket.io connection');
socket.on('event', (data) => {
console.log('socket.io data:', data);
});
socket.on('disconnect', () => {
console.log('socket.io disconnect');
});
});
config = {
module: {
loaders: [
{
test: /socket\.io\-client/,
// socket.io-client requires the window object, and navigator.userAgent to be present.
// use webpack to shim these into socket.io
loader: `imports?window=>{},navigator=>{userAgent: 'tvos'}`,
},
]
},
plugins: [
function() {
this.plugin('compile', function() {
io.emit('compile');
});
this.plugin('done', function() {
io.emit('live-reload');
});
},
],
}
@wesleyegbertsen
Copy link

How did you implement this in your tvml app/files, my co-worker is having a hard time to implement socket.io in tvml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment