@unitn-asa/deliveroo-js-sdk 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -4
- package/src/client/DjsConnect.js +27 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unitn-asa/deliveroo-js-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Software development kit for Deliveroo.js",
|
|
5
5
|
"author": "Marco Robol <marco.robol@unitn.it>",
|
|
6
6
|
"type": "module",
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
}
|
|
34
|
+
"devDependencies": {},
|
|
35
|
+
"dependencies": {}
|
|
37
36
|
}
|
package/src/client/DjsConnect.js
CHANGED
|
@@ -1,39 +1,18 @@
|
|
|
1
1
|
import { io } from "socket.io-client";
|
|
2
2
|
import { DjsClientSocket } from "./DjsClientSocket.js";
|
|
3
|
-
/**
|
|
4
|
-
* Get command-line arguments for token and name (Node.js only)
|
|
5
|
-
* Usage: npm run dev -- -token=... -name=... -host=...
|
|
6
|
-
*/
|
|
7
|
-
function getArgs() {
|
|
8
|
-
if (typeof process !== 'undefined' && process?.argv) {
|
|
9
|
-
// Dynamic import only in Node.js environment
|
|
10
|
-
try {
|
|
11
|
-
const getopts = require('getopts');
|
|
12
|
-
return getopts(process.argv, {
|
|
13
|
-
string: ['token', 'name', 'host'],
|
|
14
|
-
});
|
|
15
|
-
} catch {
|
|
16
|
-
// Fallback if require doesn't work (e.g., in bundler)
|
|
17
|
-
return { token: undefined, name: undefined, host: undefined };
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return { token: undefined, name: undefined, host: undefined };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const args = getArgs();
|
|
24
3
|
|
|
25
4
|
|
|
26
5
|
|
|
27
6
|
/**
|
|
28
7
|
* @returns { DjsClientSocket }
|
|
29
8
|
*/
|
|
30
|
-
export function DjsConnect ( host =
|
|
9
|
+
export function DjsConnect ( host = process.env.HOST || 'http://localhost:8080', token = process.env.TOKEN, name = process.env.NAME, autoconnect = true ) {
|
|
31
10
|
|
|
32
11
|
let opts = {
|
|
33
12
|
autoConnect: false,
|
|
34
13
|
withCredentials: false,
|
|
35
|
-
|
|
36
|
-
|
|
14
|
+
extraHeaders: {}, //{ 'x-token': TOKEN || token }
|
|
15
|
+
query: {}, //{ name: NAME }
|
|
37
16
|
// path: '/'
|
|
38
17
|
};
|
|
39
18
|
if ( token && token != '' )
|
|
@@ -42,12 +21,32 @@ export function DjsConnect ( host = args['host'] || process.env.HOST, token = ar
|
|
|
42
21
|
opts.query = { name: name }
|
|
43
22
|
|
|
44
23
|
const enhancedClientSocket = DjsClientSocket.enhance( io( host, opts ) );
|
|
24
|
+
|
|
25
|
+
console.log( `Connecting to ${host} ${ token ? 'with token '+ (token).substring(0,5)+'...'+(token).substring(token.length-5) : name ? 'as '+name : 'with no token and no name' }` );
|
|
45
26
|
|
|
46
|
-
|
|
47
|
-
|
|
27
|
+
enhancedClientSocket.onConnect( () => {
|
|
28
|
+
console.log( `Connected` )
|
|
29
|
+
});
|
|
48
30
|
|
|
49
|
-
|
|
31
|
+
enhancedClientSocket.onceYou( me => {
|
|
32
|
+
console.log( `Authenticated as ${me.name}(${me.id}) in team ${me.teamName}(${me.teamId})` )
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
enhancedClientSocket.on( 'disconnect', (reason) => {
|
|
36
|
+
console.log( `Disconnected from ${host} because ${reason}` )
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
enhancedClientSocket.io.on("error", (error) => {
|
|
40
|
+
console.error("Connection error:", error.message);
|
|
41
|
+
});
|
|
50
42
|
|
|
43
|
+
enhancedClientSocket.on("connect_error", (error) => {
|
|
44
|
+
console.error("Failed to connect:", error.message);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if ( autoconnect )
|
|
48
|
+
enhancedClientSocket.connect();
|
|
49
|
+
|
|
51
50
|
return enhancedClientSocket;
|
|
52
51
|
|
|
53
52
|
}
|
|
@@ -56,5 +55,5 @@ export function DjsConnect ( host = args['host'] || process.env.HOST, token = ar
|
|
|
56
55
|
|
|
57
56
|
// Example usage:
|
|
58
57
|
//
|
|
59
|
-
// const socket =
|
|
58
|
+
// const socket = DjsConnect( 'http://localhost:8080', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjczZDZkOSIsIm5hbWUiOiJub21lX21vbHRvX2x1bmdvXzEiLCJ0ZWFtSWQiOiIyNDc0ZDkiLCJ0ZWFtTmFtZSI6Im5vbWVfdGVhbV9tb2x0b19sdW5nbyIsInJvbGUiOiJ1c2VyIiwiaWF0IjoxNzQ4MzUxOTM3fQ.rhEHyAoaQhqVqhEJ5bqyu3UdcvJWK5RLWKJxkBDAX_0', true );
|
|
60
59
|
// socket.emitMove( 'up' );
|