service/tests/start_ws.js
2024-10-18 17:54:37 +08:00

66 lines
1.3 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')
s.task('task.js', 100)
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(10)
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')
u.sleep(1000)
for (let i = 0; i < 5; i++) {
let j = ws.read()
if (i !== j.data) {
return j
}
}
ws.close()
return true
}