Vim Open Channel, Use callback function
Vim ch_sendexpr(), Send data from Server to Vim client
let g:tcpChannel = ch_open('localhost:4242', {'callback': 'HaskellServer_Handler', 'mode':'json'})
↓
call ch_sendexpr(g:tcpChannel, 'hello', {'callback' : 'HaskellServer_Handler'})
↓
" TCP server side will receive the following
[1, "Hello"]
↑
msg id
" Send data from Server to Vim client from Haskell TCP Server.
" NOTE: Double quotes are IMPORTANT here
Need it in Haskell code
↓ ↓
send "[1, \"reply\"]" ✅
↑
msg id
msg id needs to match msg id from Vim client
Or msg id can be zero
send "[0, reply]" ❌
send "[0, \"reply\"]" ✅
↑
msg id
vim client TCP server
+ - - - + + - - - +
| | → 'hello' | | [1, "hello"]
+ - - - + + - - - +
| | → 'world' | | [2, "world"]
+ - - - + + - - - +
| | → 'Bye' | | [3, "Bye"]
+ - - - + + - - - +
msg id [1, 2, 3] <-> msg id [1, 2, 3]
↓ ↓
vim client TCP server
+ - - - + + - - - +
| | ← [1 'hello'] | | [1, "hello"]
+ - - - + + - - - +
| | ← [2, 'world'] | | [2, "world"]
+ - - - + + - - - +
| | ← [3, 'Bye'] | | [3, "Bye"]
+ - - - + + - - - +
msg id = 0
↓
vim client TCP server
+ - - - + + - - - +
| | ← [0 'anyab'] | | [1, "hello"]
+ - - - + + - - - +
| | ← [0, 'what '] | | [2, "world"]
+ - - - + + - - - +
| | ← [0, 'bye'] | | [3, "Bye"]
+ - - - + + - - - +
SEE: $sp/haskell-vim-channel 😀
func! HaskellServer_Init()
if exists('g:tcpChannel')
if ch_status(g:tcpChannel) == 'open'
call ch_close(g:tcpChannel)
endif
endif
sleep 1
let g:tcpChannel = ch_open('localhost:4242', {'callback': 'HaskellServer_Handler', 'mode':'json'})
echo ch_status(g:tcpChannel)
call ch_logfile('/tmp/2.x', 'w')
endfunc
func! HaskellServer_Send(mydata)
call ch_sendexpr(g:tcpChannel, a:mydata, {'callback' : 'HaskellServer_Handler'})
endfunc
func! HaskellServer_Handler(tcpChannel, msg)
echo "From handler toUpperStr => " . a:msg
call writefile([a:msg], '/tmp/aaa.x', 'a')
endfunc
" Send a list of string to the channel
func! HaskellServer_SendVisualLine()
" let ls = VisualSelectLine()
let s = @"
let xs = split(s, "\n")
let xt = ["beg"] + xs + ["end"]
call writefile([s], '/tmp/a.x', 'b')
let time = reltime()[0]
let tstr = 'time=' . time
for ux in xt
call HaskellServer_Send(ux)
endfor
endfunc