Skip to content

Commit

Permalink
<S-Enter> sends an event to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
ngn committed Aug 26, 2014
1 parent 12798a8 commit 4605a0e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
33 changes: 28 additions & 5 deletions a.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ require('socket.io').listen(server).on 'connection', (socket) ->
console.info 'browser connected'
client = require('net').connect {host: '127.0.0.1', port: 4502}, -> console.info 'interpreter connected'

toInterpreter = (s) -> console.info 'to interpreter: ' + JSON.stringify(s)[..100]; client.write rideMsg s
toBrowser = (m...) -> console.info 'to browser: ' + JSON.stringify(m)[..100]; socket.emit m...
toInterpreter = (s) -> console.info 'to interpreter: ' + JSON.stringify(s)[..1000]; client.write rideMsg s
toBrowser = (m...) -> console.info 'to browser: ' + JSON.stringify(m)[..1000]; socket.emit m...

q = Buffer 0 # a buffer for data received from the server
client.on 'data', (data) ->
q = Buffer.concat [q, data]
while q.length >= 4 and (n = q.readInt32BE 0) <= q.length
m = '' + q[8...n]
q = q[n..]
console.info 'from interpreter: ' + JSON.stringify(m)[..100]
console.info 'from interpreter: ' + JSON.stringify(m)[..1000]
if /^SupportedProtocols=/.test m
# ignore
else if /^UsingProtocol=/.test m
Expand All @@ -57,12 +57,20 @@ require('socket.io').listen(server).on 'connection', (socket) ->
toBrowser 'prompt'
else if /^<ReplyGetLog>/.test m
toBrowser 'add', b64d m.replace /^[^]*<Log>([^]*)<\/Log>[^]*$/, '$1'
else if /^<ReplyOpenWindow>/.test m
name = b64d m.replace /^[^]*<name>([^]*)<\/name>[^]*$/, '$1'
text = b64d m.replace /^[^]*<text>([^]*)<\/text>[^]*$/, '$1'
toBrowser 'open', name, text
else
console.info 'unrecognised'
return

onevent = socket.onevent
socket.onevent = (packet) ->
console.info 'from browser: ' + JSON.stringify(packet.data)[..1000]
onevent.apply socket, [packet]

socket.on 'exec', (s) ->
console.info 'from browser: ' + JSON.stringify ['exec', s]
toInterpreter """
<?xml version="1.0" encoding="utf-8"?>
<Command>
Expand All @@ -72,11 +80,26 @@ require('socket.io').listen(server).on 'connection', (socket) ->
</Command>
"""

socket.on 'edit', (s, p) -> # s:current line p:cursor position in s
toInterpreter """
<?xml version="1.0" encoding="utf-8"?>
<Command>
<cmd>Edit</cmd>
<id>0</id>
<args>
<Edit>
<Text>#{b64 s}</Text>
<Pos>#{p}</Pos>
<Win>0</Win>
</Edit>
</args>
</Command>
"""

toInterpreter 'SupportedProtocols=1'
toInterpreter 'UsingProtocol=1'
toInterpreter '<?xml version="1.0" encoding="utf-8"?><Command><cmd>Identify</cmd><id>0</id><args><Identify><Sender><Process>RIDE.EXE</Process><Proxy>0</Proxy></Sender></Identify></args></Command>'
toInterpreter '<?xml version="1.0" encoding="utf-8"?><Command><cmd>Connect</cmd><id>0</id><args><Connect><Token /></Connect></args></Command>'


client.on 'end', -> console.info 'interpreter disconnected'; socket.emit 'end'
socket.on 'disconnect', -> console.info 'browser disconnected'; client.end()
26 changes: 19 additions & 7 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
jQuery ($) ->
debug = 1

socket = io()
send = (a...) -> console.info 'send:', a...; socket.emit a...
recv = (x, f) -> socket.on x, (a...) -> console.info 'recv:', x, a...; f a...

recv 'add', (s) ->
if debug
{emit, onevent} = socket
socket.emit = (a...) -> console.info 'send:' + JSON.stringify(a)[..1000]; emit.apply socket, a
socket.onevent = (packet) -> console.info 'recv:' + JSON.stringify(packet.data)[..1000]; onevent.apply socket, [packet]

socket.on 'add', (s) ->
cm.replaceRange s, line: cm.lineCount() - 1, ch: 0

recv 'prompt', ->
socket.on 'prompt', ->
cm.replaceRange ' ', line: cm.lineCount() - 1, ch: 0
cm.setCursor cm.lineCount() - 1, 6

socket.on 'open', (name, text) ->
# todo

cm = CodeMirror document.getElementById('session'),
autofocus: true
extraKeys:
'Enter': ->
l = cm.lineCount() - 1
s = cm.getLine l
cm.replaceRange '', {line: l, ch: 0}, {line: l, ch: s.length}
send 'exec', s + '\n'
socket.emit 'exec', s + '\n'
'Shift-Enter': ->
c = cm.getCursor()
socket.emit 'edit', cm.getLine(c.line), c.ch
cm.setCursor 0, 6

# language bar
Expand Down Expand Up @@ -64,5 +75,6 @@ jQuery ($) ->

$(window).resize(-> cm.setSize null, $(window).height() - 4 - $('#lbar').height()).resize()

window.socket = socket
window.cm = cm
if debug
window.socket = socket
window.cm = cm

0 comments on commit 4605a0e

Please sign in to comment.