@soundx/ws 1.0.50

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/.fatherrc.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from 'father';
2
+
3
+ export default defineConfig({
4
+ esm: {
5
+ input: 'src',
6
+ output: 'dist/esm'
7
+ },
8
+ cjs: {
9
+ input: 'src',
10
+ output: 'dist/cjs',
11
+ transformer: 'babel',
12
+ }
13
+ });
@@ -0,0 +1,28 @@
1
+ export interface SocketConnectOptions {
2
+ /** Connection URL */
3
+ url: string;
4
+ /** Auth Token */
5
+ token: string;
6
+ /** User ID */
7
+ userId: number;
8
+ /** Device Name for identification */
9
+ deviceName: string;
10
+ /** Extra query params */
11
+ query?: Record<string, any>;
12
+ }
13
+ export declare class SharedSocketService {
14
+ private socket;
15
+ private listeners;
16
+ /**
17
+ * Initialize a connection.
18
+ * Safe to call multiple times; will ignore if already connected with same socket instance (but usually you should check connected status).
19
+ */
20
+ connect(options: SocketConnectOptions): void;
21
+ disconnect(): void;
22
+ emit(event: string, data: any): void;
23
+ on(event: string, callback: Function): void;
24
+ off(event: string, callback?: Function): void;
25
+ get id(): string | undefined;
26
+ get connected(): boolean;
27
+ }
28
+ export declare const socketService: SharedSocketService;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.socketService = exports.SharedSocketService = void 0;
7
+ var _socket = require("socket.io-client");
8
+ class SharedSocketService {
9
+ socket = null;
10
+ listeners = new Map();
11
+
12
+ /**
13
+ * Initialize a connection.
14
+ * Safe to call multiple times; will ignore if already connected with same socket instance (but usually you should check connected status).
15
+ */
16
+ connect(options) {
17
+ if (this.socket?.connected) {
18
+ // Optional: logic to disconnect if options changed significantly?
19
+ // For now, assume single connection per instance lifecycle or manual disconnect first.
20
+ return;
21
+ }
22
+ const {
23
+ url,
24
+ token,
25
+ userId,
26
+ deviceName,
27
+ query
28
+ } = options;
29
+ this.socket = (0, _socket.io)(url, {
30
+ query: {
31
+ userId,
32
+ deviceName,
33
+ ...query
34
+ },
35
+ transports: ["websocket"],
36
+ auth: {
37
+ token
38
+ }
39
+ });
40
+ this.socket.on("connect", () => {
41
+ console.log("[SharedSocket] Connected:", this.socket?.id);
42
+ });
43
+ this.socket.on("disconnect", () => {
44
+ console.log("[SharedSocket] Disconnected");
45
+ });
46
+
47
+ // Re-attach listeners if any were added before connection
48
+ this.listeners.forEach((callbacks, event) => {
49
+ callbacks.forEach(cb => this.socket?.on(event, cb));
50
+ });
51
+ }
52
+ disconnect() {
53
+ if (this.socket) {
54
+ this.socket.disconnect();
55
+ this.socket = null;
56
+ }
57
+ }
58
+ emit(event, data) {
59
+ if (this.socket?.connected) {
60
+ this.socket.emit(event, data);
61
+ } else {
62
+ console.warn("[SharedSocket] Not connected, cannot emit:", event);
63
+ }
64
+ }
65
+ on(event, callback) {
66
+ if (!this.listeners.has(event)) {
67
+ this.listeners.set(event, []);
68
+ }
69
+ this.listeners.get(event)?.push(callback);
70
+ if (this.socket) {
71
+ this.socket.on(event, callback);
72
+ }
73
+ }
74
+ off(event, callback) {
75
+ if (callback) {
76
+ const callbacks = this.listeners.get(event) || [];
77
+ const idx = callbacks.indexOf(callback);
78
+ if (idx !== -1) {
79
+ callbacks.splice(idx, 1);
80
+ }
81
+ this.socket?.off(event, callback);
82
+ } else {
83
+ this.listeners.delete(event);
84
+ this.socket?.off(event);
85
+ }
86
+ }
87
+ get id() {
88
+ return this.socket?.id;
89
+ }
90
+ get connected() {
91
+ return this.socket?.connected || false;
92
+ }
93
+ }
94
+ exports.SharedSocketService = SharedSocketService;
95
+ const socketService = exports.socketService = new SharedSocketService();
@@ -0,0 +1,28 @@
1
+ export interface SocketConnectOptions {
2
+ /** Connection URL */
3
+ url: string;
4
+ /** Auth Token */
5
+ token: string;
6
+ /** User ID */
7
+ userId: number;
8
+ /** Device Name for identification */
9
+ deviceName: string;
10
+ /** Extra query params */
11
+ query?: Record<string, any>;
12
+ }
13
+ export declare class SharedSocketService {
14
+ private socket;
15
+ private listeners;
16
+ /**
17
+ * Initialize a connection.
18
+ * Safe to call multiple times; will ignore if already connected with same socket instance (but usually you should check connected status).
19
+ */
20
+ connect(options: SocketConnectOptions): void;
21
+ disconnect(): void;
22
+ emit(event: string, data: any): void;
23
+ on(event: string, callback: Function): void;
24
+ off(event: string, callback?: Function): void;
25
+ get id(): string | undefined;
26
+ get connected(): boolean;
27
+ }
28
+ export declare const socketService: SharedSocketService;
@@ -0,0 +1,125 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
5
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
6
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
7
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
9
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
10
+ import { io } from "socket.io-client";
11
+ export var SharedSocketService = /*#__PURE__*/function () {
12
+ function SharedSocketService() {
13
+ _classCallCheck(this, SharedSocketService);
14
+ _defineProperty(this, "socket", null);
15
+ _defineProperty(this, "listeners", new Map());
16
+ }
17
+ _createClass(SharedSocketService, [{
18
+ key: "connect",
19
+ value:
20
+ /**
21
+ * Initialize a connection.
22
+ * Safe to call multiple times; will ignore if already connected with same socket instance (but usually you should check connected status).
23
+ */
24
+ function connect(options) {
25
+ var _this$socket,
26
+ _this = this;
27
+ if ((_this$socket = this.socket) !== null && _this$socket !== void 0 && _this$socket.connected) {
28
+ // Optional: logic to disconnect if options changed significantly?
29
+ // For now, assume single connection per instance lifecycle or manual disconnect first.
30
+ return;
31
+ }
32
+ var url = options.url,
33
+ token = options.token,
34
+ userId = options.userId,
35
+ deviceName = options.deviceName,
36
+ query = options.query;
37
+ this.socket = io(url, {
38
+ query: _objectSpread({
39
+ userId: userId,
40
+ deviceName: deviceName
41
+ }, query),
42
+ transports: ["websocket"],
43
+ auth: {
44
+ token: token
45
+ }
46
+ });
47
+ this.socket.on("connect", function () {
48
+ var _this$socket2;
49
+ console.log("[SharedSocket] Connected:", (_this$socket2 = _this.socket) === null || _this$socket2 === void 0 ? void 0 : _this$socket2.id);
50
+ });
51
+ this.socket.on("disconnect", function () {
52
+ console.log("[SharedSocket] Disconnected");
53
+ });
54
+
55
+ // Re-attach listeners if any were added before connection
56
+ this.listeners.forEach(function (callbacks, event) {
57
+ callbacks.forEach(function (cb) {
58
+ var _this$socket3;
59
+ return (_this$socket3 = _this.socket) === null || _this$socket3 === void 0 ? void 0 : _this$socket3.on(event, cb);
60
+ });
61
+ });
62
+ }
63
+ }, {
64
+ key: "disconnect",
65
+ value: function disconnect() {
66
+ if (this.socket) {
67
+ this.socket.disconnect();
68
+ this.socket = null;
69
+ }
70
+ }
71
+ }, {
72
+ key: "emit",
73
+ value: function emit(event, data) {
74
+ var _this$socket4;
75
+ if ((_this$socket4 = this.socket) !== null && _this$socket4 !== void 0 && _this$socket4.connected) {
76
+ this.socket.emit(event, data);
77
+ } else {
78
+ console.warn("[SharedSocket] Not connected, cannot emit:", event);
79
+ }
80
+ }
81
+ }, {
82
+ key: "on",
83
+ value: function on(event, callback) {
84
+ var _this$listeners$get;
85
+ if (!this.listeners.has(event)) {
86
+ this.listeners.set(event, []);
87
+ }
88
+ (_this$listeners$get = this.listeners.get(event)) === null || _this$listeners$get === void 0 || _this$listeners$get.push(callback);
89
+ if (this.socket) {
90
+ this.socket.on(event, callback);
91
+ }
92
+ }
93
+ }, {
94
+ key: "off",
95
+ value: function off(event, callback) {
96
+ if (callback) {
97
+ var _this$socket5;
98
+ var callbacks = this.listeners.get(event) || [];
99
+ var idx = callbacks.indexOf(callback);
100
+ if (idx !== -1) {
101
+ callbacks.splice(idx, 1);
102
+ }
103
+ (_this$socket5 = this.socket) === null || _this$socket5 === void 0 || _this$socket5.off(event, callback);
104
+ } else {
105
+ var _this$socket6;
106
+ this.listeners.delete(event);
107
+ (_this$socket6 = this.socket) === null || _this$socket6 === void 0 || _this$socket6.off(event);
108
+ }
109
+ }
110
+ }, {
111
+ key: "id",
112
+ get: function get() {
113
+ var _this$socket7;
114
+ return (_this$socket7 = this.socket) === null || _this$socket7 === void 0 ? void 0 : _this$socket7.id;
115
+ }
116
+ }, {
117
+ key: "connected",
118
+ get: function get() {
119
+ var _this$socket8;
120
+ return ((_this$socket8 = this.socket) === null || _this$socket8 === void 0 ? void 0 : _this$socket8.connected) || false;
121
+ }
122
+ }]);
123
+ return SharedSocketService;
124
+ }();
125
+ export var socketService = new SharedSocketService();
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@soundx/ws",
3
+ "version": "1.0.50",
4
+ "description": "Shared WebSocket client for SoundX",
5
+ "author": "mmdctjj <mmdctjj@gmail.com>",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/mmdctjj/AudioDock"
9
+ },
10
+ "homepage": "https://github.com/mmdctjj/AudioDock",
11
+ "main": "dist/cjs/index.js",
12
+ "module": "dist/esm/index.js",
13
+ "scripts": {
14
+ "dev": "father dev",
15
+ "build": "father build",
16
+ "prepublishOnly": "father doctor && npm run build"
17
+ },
18
+ "types": "dist/esm/index.d.ts",
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "socket.io-client": "^4.8.1"
24
+ },
25
+ "devDependencies": {
26
+ "father": "^4.5.3",
27
+ "typescript": "^5.0.0"
28
+ },
29
+ "peerDependencies": {
30
+ "socket.io-client": "^4.8.1"
31
+ }
32
+ }
package/src/index.ts ADDED
@@ -0,0 +1,108 @@
1
+ import { io, Socket } from "socket.io-client";
2
+
3
+ export interface SocketConnectOptions {
4
+ /** Connection URL */
5
+ url: string;
6
+ /** Auth Token */
7
+ token: string;
8
+ /** User ID */
9
+ userId: number;
10
+ /** Device Name for identification */
11
+ deviceName: string;
12
+ /** Extra query params */
13
+ query?: Record<string, any>;
14
+ }
15
+
16
+ export class SharedSocketService {
17
+ private socket: Socket | null = null;
18
+ private listeners: Map<string, Function[]> = new Map();
19
+
20
+ /**
21
+ * Initialize a connection.
22
+ * Safe to call multiple times; will ignore if already connected with same socket instance (but usually you should check connected status).
23
+ */
24
+ connect(options: SocketConnectOptions) {
25
+ if (this.socket?.connected) {
26
+ // Optional: logic to disconnect if options changed significantly?
27
+ // For now, assume single connection per instance lifecycle or manual disconnect first.
28
+ return;
29
+ }
30
+
31
+ const { url, token, userId, deviceName, query } = options;
32
+
33
+ this.socket = io(url, {
34
+ query: {
35
+ userId,
36
+ deviceName,
37
+ ...query
38
+ },
39
+ transports: ["websocket"],
40
+ auth: {
41
+ token,
42
+ }
43
+ });
44
+
45
+ this.socket.on("connect", () => {
46
+ console.log("[SharedSocket] Connected:", this.socket?.id);
47
+ });
48
+
49
+ this.socket.on("disconnect", () => {
50
+ console.log("[SharedSocket] Disconnected");
51
+ });
52
+
53
+ // Re-attach listeners if any were added before connection
54
+ this.listeners.forEach((callbacks, event) => {
55
+ callbacks.forEach(cb => this.socket?.on(event, cb as any));
56
+ });
57
+ }
58
+
59
+ disconnect() {
60
+ if (this.socket) {
61
+ this.socket.disconnect();
62
+ this.socket = null;
63
+ }
64
+ }
65
+
66
+ emit(event: string, data: any) {
67
+ if (this.socket?.connected) {
68
+ this.socket.emit(event, data);
69
+ } else {
70
+ console.warn("[SharedSocket] Not connected, cannot emit:", event);
71
+ }
72
+ }
73
+
74
+ on(event: string, callback: Function) {
75
+ if (!this.listeners.has(event)) {
76
+ this.listeners.set(event, []);
77
+ }
78
+ this.listeners.get(event)?.push(callback);
79
+
80
+ if (this.socket) {
81
+ this.socket.on(event, callback as any);
82
+ }
83
+ }
84
+
85
+ off(event: string, callback?: Function) {
86
+ if (callback) {
87
+ const callbacks = this.listeners.get(event) || [];
88
+ const idx = callbacks.indexOf(callback);
89
+ if (idx !== -1) {
90
+ callbacks.splice(idx, 1);
91
+ }
92
+ this.socket?.off(event, callback as any);
93
+ } else {
94
+ this.listeners.delete(event);
95
+ this.socket?.off(event);
96
+ }
97
+ }
98
+
99
+ get id() {
100
+ return this.socket?.id;
101
+ }
102
+
103
+ get connected() {
104
+ return this.socket?.connected || false;
105
+ }
106
+ }
107
+
108
+ export const socketService = new SharedSocketService();
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "declaration": true,
5
+ "skipLibCheck": true,
6
+ "baseUrl": "./",
7
+ "outDir": "dist",
8
+ "rootDir": "src",
9
+ "lib": ["es2015", "dom"]
10
+ },
11
+ "include": ["src"]
12
+ }