10.3.4 • Published 24 days ago

ag-sockets v10.3.4

Weekly downloads
188
License
MIT
Repository
github
Last release
24 days 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
10.3.4

24 days ago

10.3.3

27 days ago

10.3.2

9 months ago

10.3.1

1 year ago

10.2.0

1 year ago

10.3.0

1 year ago

9.0.5

2 years ago

9.0.4

2 years ago

9.0.3

2 years ago

10.0.0

2 years ago

9.1.2

2 years ago

9.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

9.2.3

2 years ago

9.2.2

2 years ago

9.2.1

2 years ago

9.1.1

2 years ago

9.1.0

2 years ago

9.3.2

2 years ago

9.3.1

2 years ago

9.3.0

2 years ago

10.1.0

1 year ago

9.2.0

2 years ago

9.4.0

2 years ago

7.8.2

2 years ago

8.1.0

2 years ago

8.0.0

2 years ago

7.8.0

3 years ago

7.8.1

3 years ago

7.6.0

3 years ago

7.7.1

3 years ago

7.7.0

3 years ago

7.5.0

3 years ago

7.4.0

3 years ago

7.3.2

3 years ago

7.3.1

3 years ago

7.3.0

3 years ago

7.2.0

3 years ago

7.1.0

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.8.7

3 years ago

6.8.6

3 years ago

6.8.5

3 years ago

6.8.3

3 years ago

6.8.4

3 years ago

6.8.2

3 years ago

6.8.1

3 years ago

6.8.0

3 years ago

6.7.1

3 years ago

6.7.0

4 years ago

6.6.0

4 years ago

6.5.2

4 years ago

6.5.1

4 years ago

6.5.0

4 years ago

6.4.2

4 years ago

6.4.1

4 years ago

6.4.0

4 years ago

6.3.0

4 years ago

6.3.2

4 years ago

6.3.1

4 years ago

6.2.0

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

5.9.0

4 years ago

5.8.0

4 years ago

5.7.1

4 years ago

5.7.0

4 years ago

5.6.0

4 years ago

5.5.0

4 years ago

5.4.3

4 years ago

5.4.2

4 years ago

5.4.1

4 years ago

5.4.0

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.0

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.7.0

5 years ago

4.6.0

5 years ago

4.5.2

5 years ago

4.5.1

5 years ago

4.5.0

5 years ago

4.4.0

5 years ago

4.3.3

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.6

5 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.21.1

6 years ago

1.21.0

6 years ago

1.20.0

6 years ago

1.19.3

6 years ago

1.19.2

6 years ago

1.19.1

6 years ago

1.19.0

6 years ago

1.18.0

6 years ago

1.17.2

6 years ago

1.17.1

6 years ago

1.17.0

6 years ago

1.16.1

6 years ago

1.16.0

6 years ago

1.15.0

6 years ago

1.14.0

6 years ago

1.13.0

6 years ago

1.12.0

6 years ago

1.11.0

6 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

7 years ago

1.6.2

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.8.9

7 years ago

0.8.8

7 years ago

0.8.7

7 years ago

0.8.6

7 years ago

0.8.5

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.6.6

8 years ago

0.6.5

8 years ago

0.6.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago