summaryrefslogtreecommitdiffstats
path: root/replmain.tcl
blob: 152e0de5171a47b5d7c5bf5fb9fefe68ad97bd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
source "hosts.tcl"

lappend auto_path "./vendor"
package require websocket

proc handleWs {sock type msg} {
    if {$type eq "connect"} {
        puts stderr "sharer: Connected"
        repl
    } elseif {$type eq "disconnect"} {
        puts stderr "sharer: Disconnected"
        after 2000 { setupSock }
    } else {
        set ::response $msg
    }
}

proc setupSock {} {
    puts stderr "sharer: Trying to connect to: ws://$::shareNode:4273/ws"
    set ::sock [::websocket::open "ws://$::shareNode:4273/ws" handleWs]
}
setupSock

proc repl {} {
    set prompt "% "
    while 1 {
        puts -nonewline $prompt
        flush stdout
        gets stdin line        ;# read...
        if [eof stdin] break
        ::websocket::send $::sock text [list apply {{line} {
            upvar chan chan
            ::websocket::send $chan text [eval $line]
        }} $line]
        vwait ::response
        puts $::response
    }
}

vwait forever