Apache TinkerPop 的 Gremlin Server 使用 Websocket 或者是 HTTP 協定與 client 溝通。 如果是 Websocket,使用的指令如下:
bin/gremlin-server.sh conf/gremlin-server-classic.yaml
而使用者可以透過 Websocket 傳送 script string 給 Gremlin Server 執行,並且取得執行的結果。
Tcllib 提供了 websocket server 與 client 的實作,這裡測試 client 的部份。 下面就是使用 Tcl 寫的測試程式(改寫自 Tcllib 文件的範例):
package require websocket
package require uuid
::websocket::loglevel debug
proc handler { sock type msg } {
switch -glob -nocase -- $type {
co* {
puts "Connected on $sock"
}
te* {
puts "RECEIVED: $msg"
}
cl* -
dis* {
}
}
}
proc test { sock } {
puts "[::websocket::conninfo $sock type] from \
[::websocket::conninfo $sock sockname] to \
[::websocket::conninfo $sock peername]"
set id [::uuid::uuid generate]
set msg "{\"requestId\":\"$id\",
\"op\":\"eval\",
\"processor\":\"\",
\"args\":{\"gremlin\":\"g.V(x).out()\",
\"bindings\":{\"x\":1},
\"language\":\"gremlin-groovy\"}}"
::websocket::send $sock text $msg
}
set sock [::websocket::open ws://localhost:8182/gremlin handler]
after 400 test $sock
vwait forever
Gremlin server 除了支援 TEXT type 的訊息,也支援 BINARY type 的訊息,下面是相關的測試:
package require websocket
package require uuid
::websocket::loglevel debug
proc handler { sock type msg } {
switch -glob -nocase -- $type {
co* {
puts "Connected on $sock"
}
bi* {
puts "RECEIVED: $msg"
set ::received 1
}
cl* -
dis* {
}
}
}
proc test { sock } {
puts "[::websocket::conninfo $sock type] from \
[::websocket::conninfo $sock sockname] to \
[::websocket::conninfo $sock peername]"
set id [::uuid::uuid generate]
set msg "{\"requestId\":\"$id\",
\"op\":\"eval\",
\"processor\":\"\",
\"args\":{\"gremlin\":\"g.V(x).out()\",
\"bindings\":{\"x\":1},
\"language\":\"gremlin-groovy\"}}"
set mimetype "application/json"
set length [binary format c [string length $mimetype]]
set finalmsg [string cat $length $mimetype $msg]
::websocket::send $sock binary $finalmsg
}
set sock [::websocket::open ws://localhost:8182/gremlin handler]
after 400 test $sock
set received 0
vwait received
after 100
::websocket::close $sock 1000
沒有留言:
張貼留言