This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
87 lines (73 loc) · 2.34 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs')
const util = require('util')
const WebSocket = require('ws')
const uuid4 = require('uuid/v4')
const yargs = require('yargs')
async function loadImage(filename) {
const readFileAsync = util.promisify(fs.readFile)
return readFileAsync(filename)
}
function handshake(clientId, socket) {
return new Promise((resolve, reject) => {
socket.once('open', () => {
console.log('WebSocket connected, sending handshake')
socket.send(JSON.stringify({
data: {
agent: `WebSocketCli/0.1.0 (node ${process.version})`,
clientId: clientId
},
op: 2
}))
})
socket.once('message', (data) => {
const res = JSON.parse(data)
if (res.op != 2) {
console.error('Received non-handshake response')
reject('handshake failed')
}
console.info(`Session established, SessionId: ${res.data.sessionId}`)
resolve(res.data.sessionId)
})
})
}
async function main(endpoint, filename) {
console.log(`Reading image file: ${filename}`)
const image = await loadImage(filename)
console.log(`Connecting to endpoint: ${endpoint}`)
const socket = new WebSocket(endpoint)
socket.on('close', (code, reason) => {
console.error(`Connection closed: ${code} ${reason}`)
process.exit(-1)
})
const clientId = uuid4()
const sessionId = await handshake(clientId, socket)
socket.on('message', (data) => console.log(data))
setInterval(() => socket.send(JSON.stringify({
data: {
sessionId: sessionId
},
op: 3
})), 500)
setTimeout(() => {
console.info('Sending image recognition request')
socket.send(JSON.stringify({
data: {
contentLength: image.length,
contentType: 'image/jpeg'
},
op: 5
}))
}, 500)
setTimeout(() => socket.send(JSON.stringify({
op: 1
})), 5000)
}
(function entry() {
var args = yargs
.alias('u', 'endpoint')
.default('endpoint', 'ws://localhost:51997/api/v2/websocket')
.alias('f', 'filename')
.default('filename', 'image.jpg')
.argv
main(args.endpoint, args.filename)
})()