http/ws_test.js

66 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-10-15 10:45:04 +08:00
import http from 'apigo.cc/gojs/http'
import console from 'apigo.cc/gojs/console'
function main() { }
function testSync() {
let conn = http.connect("http://127.0.0.1:18001/ws")
conn.write('111')
if (conn.read() !== '111') {
return false
}
conn.write(new Uint8Array([0x01, 0x02, 0x03]))
let r2 = conn.read()
if (r2[0] != 1 || r2[1] != 2 || r2[2] != 3) {
return false
}
conn.writeJSON({ name: 'Tom' })
let r3 = conn.readJSON()
if (r3.name != 'Tom') {
return false
}
conn.close()
return true
}
let asyncResult = ''
let asyncConn
function testAsync() {
let conn = http.connect("ws://127.0.0.1:18001/ws", {
pingInterval: 10,
reconnectInterval: 10,
compress: true,
onError: console.error,
onClose: function (code, text) {
console.info("onClose", code, text)
},
onPing: function (data) {
console.info("onPing", data)
},
onPong: function (data) {
console.info("onPong", data)
},
onJSONMessage: function (data) {
// console.info("onJSONMessage", data)
asyncResult += data.name
},
})
asyncConn = conn
conn.writeJSON({ name: 'Tom1' })
conn.writeJSON({ name: 'Tom2' })
conn.writeJSON({ name: 'Tom3' })
}
function testAsync2() {
asyncConn.writeJSON({ name: 'Tom4' })
asyncConn.writeJSON({ name: 'Tom5' })
asyncConn.writeJSON({ name: 'Tom6' })
}
function closeAsync() {
asyncConn.close()
}