Created
June 17, 2015 17:19
-
-
Save xaviervia/cb7cdfb3f06375a11e85 to your computer and use it in GitHub Desktop.
Remote REPL with WebSockets
This file contains hidden or 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
(function (window) { | |
'use strict' | |
var Socket = function (url) { | |
this.url = url || 'ws://localhost:10001' | |
this.connect() | |
} | |
Socket.prototype.connect = function () { | |
try { | |
var socket = new WebSocket(this.url) | |
socket.onmessage = function (message) { | |
try { | |
socket.send(JSON.stringify(eval(message.data))) | |
} catch (e) { | |
socket.send(JSON.stringify({ | |
"error": { | |
"message": e.message, | |
"trace": e.trace | |
} | |
})) | |
} | |
} | |
this.socket = socket | |
} | |
catch (e) { | |
var that = this | |
setTimeout(function () { | |
console.warn("Reconnection to remote REPL on " + that.url) | |
that.connect() | |
}, 1000) | |
} | |
} | |
window.Socket = Socket | |
})(window) |
This file contains hidden or 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
<!doctype html> | |
<script src="repl.js" charset="utf-8"></script> | |
<script> | |
var socket = new Socket() | |
</script> |
This file contains hidden or 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
{ | |
"name": "remote-repl", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "coffee repl.coffee" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"coffee-script": "1.9.3", | |
"faye-websocket": "0.9.4" | |
} | |
} |
This file contains hidden or 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
WebSocket = require 'faye-websocket' | |
http = require 'http' | |
readline = require 'readline' | |
server = http.createServer() | |
server.on 'upgrade', (request, socket, body) -> | |
if WebSocket.isWebSocket request | |
ws = new WebSocket request, socket, body | |
rl = readline.createInterface | |
input: process.stdin, | |
output: process.stdout | |
rl.question "> ", (command) -> | |
ws.send command | |
ws.on 'message', (event) -> | |
try | |
console.log JSON.parse event.data | |
catch | |
console.log event.data | |
rl.question "> ", (command) -> | |
ws.send command | |
ws.on 'close', (event) -> | |
console.log 'close', event.code, event.reason | |
rl.close() | |
ws = null | |
server.listen 10001, -> | |
console.log 'Listening on port 10001' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment