service/tests/start_ws.js

57 lines
1.1 KiB
JavaScript

import s from "apigo.cc/gojs/service"
import http from "apigo.cc/gojs/http"
import u from "apigo.cc/gojs/util"
import co from "apigo.cc/gojs/console"
let hc = http
let urlPrefix
function main() {
s.load('api/ws.js')
let host = s.start()
hc = http.new({ baseURL: 'http://' + host })
return host
}
function testWS() {
let ws = hc.connect('/ws', { pingInterval: 10 })
let r = ws.read()
if (r.data !== 'Hello, World!') {
ws.close()
return r
}
co.info('test ws hello ok')
ws.write('abc')
r = ws.read()
if (r.data !== 'abc') {
ws.close()
return r
}
co.info('test ws abc ok')
// ws.ping()
u.sleep(100)
let pc = ws.pingCount()
co.info('test ws ping ok', pc.pingTimes, pc.pongTimes)
ws.write(new Uint8Array([1, 2, 3]))
r = ws.read()
if (r.data[0] !== 1 || r.data[1] !== 2 || r.data[2] !== 3) {
ws.close()
return r
}
co.info('test ws bytes ok')
ws.write({ name: 'Tom' })
r = ws.read()
if (r.data.name !== 'Tom') {
ws.close()
return r
}
co.info('test ws json ok')
ws.close()
return true
}