Last active
August 29, 2017 07:45
-
-
Save joshhunt/ff0f549f6f5e89ba040a to your computer and use it in GitHub Desktop.
Socket.io live reload for Apple TV TVML app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'babel-polyfill'; | |
import firstView from 'views/first'; | |
import * as liveReload from 'lib/liveReload'; | |
App.onLaunch((launchOptions) => { | |
firstView(); | |
liveReload.connect(launchOptions); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || {}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); | |
}, | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nevermind , it seems I was hitting my web-pack-dev-server and that one does not have a socket.io client.
I changed the port and all is well.