13.0.0 • Published 9 months ago

ag-sockets v13.0.0

Weekly downloads
188
License
MIT
Repository
github
Last release
9 months ago

ag-sockets

Build Status npm version

Library for communication via WebSockets

Installation

npm install ag-sockets
npm install ws # peer dependency

Usage

Using with µWebSockets

npm install uws
import * as uws from 'uws';
import { createServer } from 'ag-sockets';

// ...
const wsServer = createServer(..., { ws: uws, arrayBuffer: true });

Set up sockets

Common interfaces

// interfaces.ts

import { SocketClient, SocketServer } from 'ag-sockets';

export interface IExampleClient extends SocketClient {
  message(name: string, message: string);
}

export interface IExampleServer extends SocketServer {
  connected(): void;
  diconnected(): void;
  broadcast(message: string): void;
  setName(name: string): void;
}

Client object

// client.ts

import { Method } from 'ag-sockets';
import { IExampleClient, IExampleServer } from './interfaces';

export class ExampleClient implements IExampleClient {
  constructor(private server: IExampleServer) {
  }
  connected() {
    this.server.setName('John');
    this.server.broadcast('Hello!');
  }
  @Method()
  message(name: string, message: string) {
    console.log(`${name}: ${message}`);
  }
}

Server object

// server.ts

import { Method, Socket, ClientExtensions } from 'ag-sockets';
import { IExampleClient, IExampleServer } from './interfaces';

const clients: ExampleClient[] = [];

@Socket({ path: '/test' })
export class ExampleServer implements IExampleServer {
  private name: string;
  constructor(private client: IExampleClient & ClientExtensions) {
  }
  connected() {
    clients.push(this.client);
  }
  disconnected() {
    clients.splice(clients.indexOf(this.client), 1);
  }
  @Method() // annotations are optional if all class methods are to be available
  broadcast(message: string) {
    clients.forEach(c => c.message(this.name, message));
  }
  @Method()
  setName(name: string) {
    this.name = name;
  }
}

Start server

import * as http from 'http';
import { createServer } from 'ag-sockets';
import { ExampleClient } from './client';
import { ExampleServer } from './server';

const server = http.createServer();
const wsServer = createServer(server, ExampleServer, ExampleClient, client => new Server(client));

// pass 'wsServer.options()' to client side

server.listen(12345, () => console.log('server listening...'));

Connect client

import { createClientSocket } from 'ag-sockets';
import { ExampleClient } from './client';
import { IExampleClient, IExampleServer } from './interfaces';

const options = // get 'wsServer.options()' from server side
const service = createClientSocket<IExampleClient, IExampleServer>(options);
service.client = new ExampleClient(service.server);
service.connect();

Binary communication

export const enum Bin {
	I8,
	U8,
	I16,
	U16,
	I32,
	U32,
	F32,
	F64,
	Bool,
	Str,
	Obj,
}

// examples

class Client {
	@Method({ binary: [Bin.I32, Bin.Str] })
	foo(a: number, b: string) {
	}
	@Method({ binary: [[Bin.I32], [Bin.I32, Bin.I32, Bin.I32]] })
	bar(a: number[], b: [number, number, number][]) {
	}
	@Method({ binary: [[Bin.F32], Bin.Obj] })
	boo(a: number[], b: any) {
	}
}

Development

gulp build          # build production version of code

gulp dev            # build and start watch tasks
gulp dev --tests    # build and start watch tasks with tests
gulp dev --coverage # build and start watch tasks with tests and code coverage

gulp lint           # typescript lint
13.0.0

9 months ago

12.0.0

10 months ago

11.0.1

1 year ago

11.0.0

2 years ago

10.3.4

2 years ago

10.3.3

2 years ago

10.3.2

2 years ago

10.3.1

3 years ago

10.2.0

3 years ago

10.3.0

3 years ago

9.0.5

4 years ago

9.0.4

4 years ago

9.0.3

4 years ago

10.0.0

3 years ago

9.1.2

3 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

9.2.3

3 years ago

9.2.2

3 years ago

9.2.1

3 years ago

9.1.1

4 years ago

9.1.0

4 years ago

9.3.2

3 years ago

9.3.1

3 years ago

9.3.0

3 years ago

10.1.0

3 years ago

9.2.0

3 years ago

9.4.0

3 years ago

7.8.2

4 years ago

8.1.0

4 years ago

8.0.0

4 years ago

7.8.0

4 years ago

7.8.1

4 years ago

7.6.0

4 years ago

7.7.1

4 years ago

7.7.0

4 years ago

7.5.0

4 years ago

7.4.0

4 years ago

7.3.2

5 years ago

7.3.1

5 years ago

7.3.0

5 years ago

7.2.0

5 years ago

7.1.0

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.8.7

5 years ago

6.8.6

5 years ago

6.8.5

5 years ago

6.8.3

5 years ago

6.8.4

5 years ago

6.8.2

5 years ago

6.8.1

5 years ago

6.8.0

5 years ago

6.7.1

5 years ago

6.7.0

5 years ago

6.6.0

5 years ago

6.5.2

5 years ago

6.5.1

5 years ago

6.5.0

5 years ago

6.4.2

5 years ago

6.4.1

5 years ago

6.4.0

5 years ago

6.3.0

5 years ago

6.3.2

5 years ago

6.3.1

5 years ago

6.2.0

5 years ago

6.1.0

5 years ago

6.0.0

5 years ago

5.9.0

6 years ago

5.8.0

6 years ago

5.7.1

6 years ago

5.7.0

6 years ago

5.6.0

6 years ago

5.5.0

6 years ago

5.4.3

6 years ago

5.4.2

6 years ago

5.4.1

6 years ago

5.4.0

6 years ago

5.3.1

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.7.0

7 years ago

4.6.0

7 years ago

4.5.2

7 years ago

4.5.1

7 years ago

4.5.0

7 years ago

4.4.0

7 years ago

4.3.3

7 years ago

4.3.2

7 years ago

4.3.1

7 years ago

4.3.0

7 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.4

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.21.1

7 years ago

1.21.0

7 years ago

1.20.0

7 years ago

1.19.3

7 years ago

1.19.2

8 years ago

1.19.1

8 years ago

1.19.0

8 years ago

1.18.0

8 years ago

1.17.2

8 years ago

1.17.1

8 years ago

1.17.0

8 years ago

1.16.1

8 years ago

1.16.0

8 years ago

1.15.0

8 years ago

1.14.0

8 years ago

1.13.0

8 years ago

1.12.0

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.2

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.4

9 years ago

1.2.3

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.8.9

9 years ago

0.8.8

9 years ago

0.8.7

9 years ago

0.8.6

9 years ago

0.8.5

9 years ago

0.8.4

9 years ago

0.8.3

9 years ago

0.8.2

9 years ago

0.8.1

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.6

9 years ago

0.6.5

9 years ago

0.6.4

9 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.0

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.16

9 years ago

0.1.15

9 years ago

0.1.14

10 years ago

0.1.13

10 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.4

12 years ago

0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago