Skip to content
Permalink
Browse files

.

  • Loading branch information...
yandeu committed Apr 24, 2019
0 parents commit a4d2e11d8092aced2b5878d3b376d68da7154f5a
Showing with 11,915 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. +6 −0 .prettierrc
  3. +29 −0 LICENSE
  4. +195 −0 README.md
  5. +123 −0 cheatsheet.md
  6. +61 −0 dev/chatApp/client.ts
  7. +35 −0 dev/chatApp/index.html
  8. +50 −0 dev/chatApp/server.ts
  9. +12 −0 dev/tsconfig.json
  10. +44 −0 dev/webpack/webpack.client.js
  11. +30 −0 dev/webpack/webpack.server.js
  12. +7,321 −0 package-lock.json
  13. +65 −0 package.json
  14. +3 −0 packages/client/.npmignore
  15. +5 −0 packages/client/README.md
  16. +20 −0 packages/client/package-lock.json
  17. +30 −0 packages/client/package.json
  18. +108 −0 packages/client/src/client.ts
  19. +5 −0 packages/client/src/index.ts
  20. +102 −0 packages/client/src/wrtc/connectionsManager.ts
  21. +29 −0 packages/client/src/wrtc/peerConnection.ts
  22. +23 −0 packages/client/tsconfig.json
  23. +3 −0 packages/common/.npmignore
  24. +5 −0 packages/common/README.md
  25. +13 −0 packages/common/package-lock.json
  26. +29 −0 packages/common/package.json
  27. +32 −0 packages/common/src/bridge.ts
  28. +22 −0 packages/common/src/constants.ts
  29. +21 −0 packages/common/src/helpers.ts
  30. +25 −0 packages/common/src/parseMessage.ts
  31. +20 −0 packages/common/src/sendMessage.ts
  32. +36 −0 packages/common/src/typings.ts
  33. +23 −0 packages/common/tsconfig.json
  34. +3 −0 packages/server/.npmignore
  35. +5 −0 packages/server/README.md
  36. +838 −0 packages/server/package-lock.json
  37. +35 −0 packages/server/package.json
  38. +120 −0 packages/server/src/httpServer/httpServer.ts
  39. +20 −0 packages/server/src/httpServer/parseBody.ts
  40. +11 −0 packages/server/src/httpServer/setCors.ts
  41. +11 −0 packages/server/src/iceServers.ts
  42. +6 −0 packages/server/src/index.ts
  43. +120 −0 packages/server/src/server.ts
  44. +175 −0 packages/server/src/wrtc/channel.ts
  45. +108 −0 packages/server/src/wrtc/connectionsManager.ts
  46. +25 −0 packages/server/src/wrtc/defaultConnection.ts
  47. +132 −0 packages/server/src/wrtc/webrtcConnection.ts
  48. +23 −0 packages/server/tsconfig.json
  49. BIN readme/logo-256.png
  50. BIN readme/logo-512.png
  51. +1,750 −0 readme/logo.svg
@@ -0,0 +1,8 @@
/.vscode
/dev/dist
/node_modules
*node_modules*
/lib
*/client/lib*
*/common/lib*
*/server/lib*
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"semi": false,
"singleQuote": true,
"trailingComma": "none"
}
29 LICENSE
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019, Yannick Deubel (https://github.com/yandeu)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
195 README.md
@@ -0,0 +1,195 @@
<div align="center">

<a href="http://geckos.io">
<img src="readme/logo-256.png" alt="logo" width="128">
</a>

# geckos.io

#### Geckos&#46;io offers real-time client/server communication over UDP using WebRTC and Node.js

[![Dependency Status](https://david-dm.org/geckosio/geckos.io.svg)](https://david-dm.org/geckosio/geckos.io)
[![devDependency Status](https://david-dm.org/geckosio/geckos.io/dev-status.svg)](https://david-dm.org/geckosio/geckos.io?type=dev)
[![NPM version](https://img.shields.io/npm/v/geckos.io.svg?color=%230fc625)](https://www.npmjs.com/package/@geckos.io/server)
[![Downloads](https://img.shields.io/npm/dm/geckos.io.svg?style=flat)](https://www.npmjs.com/package/@geckos.io/server)

</div>

---

## What is it made for?

It's designed specifically for your HTML5 real-time multiplayer games by lowering the average latency and preventing huge latency spikes.

## Getting Started

First things first, install it via npm:

```console
npm install @geckos.io/client @geckos.io/server
```

## Usage

#### client.js

```js
import geckos from '@geckos.io/client'
const channel = geckos()
channel.onConnect(error => {
if (error) {
console.error(error.message)
return
}
channel.on('chat message', data => {
console.log(`You got the message ${data}`)
})
channel.emit('chat message', 'a short message sent to the server')
})
```

#### server.js

```js
const geckos = require('@geckos.io/server').default
// or with es6
import geckos from '@geckos.io/server'
const io = geckos()
io.listen()
io.onConnection(channel => {
channel.onDisconnect(() => {
console.log(`${channel.id} got disconnected`)
})
channel.on('chat message', data => {
console.log(`got ${data} from "chat message"`)
// emit the "chat message" data to all channels in the same room
io.room(channel.roomId).emit('chat message', data)
})
})
```

## Cheatsheet

You will find all the available methos in the [cheatsheet](cheatsheet.md)!

## Servers

### Standalone

```js
import geckos from '@geckos.io/server'
const io = geckos()
io.onConnection( channel => { ... })
io.listen()
```

### Node.js HTTP Server

```js
// TODO(yandeu) addServer() does not work yet
const geckos = require('@geckos.io/server').default
const http = require('http')
const server = http.createServer()
const io = geckos()
io.addServer(server)
io.onConnection( channel => { ... })
server.listen(3000)
```

### Express

```js
// TODO(yandeu) addServer() does not work yet
const geckos = require('@geckos.io/server').default
const http = require('http')
const express = require('express')
const app = express()
const server = http.createServer(app)
const io = geckos()
io.addServer(server)
io.onConnection( channel => { ... })
server.listen(3000);
```

## Deployment

You have to make sure you deploy it to a server which forwards all traffic on ports **9208/tcp** and **0-65535/upd** to your application.

Port 9208/tcp is used for the peer signaling. The peer connection itself will be on a random port between 0-65535/upd.

## ICE Servers

Geckos&#46;io provides a default list of ICE servers for testing. In production, you should probably use your own STUN and TURN servers.

```js
const geckos = require('@geckos.io/server').default
const { iceServers } = require('@geckos.io/server')
// or
import geckos, { iceServers } from '@geckos.io/server'
// use an empty array if you are developing locally
// use the default iceServers if you are testing it on your server
const io = geckos(null, TESTING_LOCALLY ? [] : iceServers)
```

## TypeScript

Geckos&#46;io is written in TypeScript. If you import geckos&#46;io with the `import` statement, the types will be imported as well.

```ts
// client.js
import geckos, { Data } from '@geckos.io/client'
const channel = geckos('YOUR_SERVER_URL')
channel.onConnect(() => {
channel.on('chat message', (data: Data) => {
// ...
})
})
// server.js
import geckos, { Data, Channel } from '@geckos.io/server'
const io = geckos()
io.onConnection((channel: Channel) => {
channel.on('chat message', (data: Data) => {
// ...
})
})
```

## Examples

- [Multiplayer Game with phaser.io](https://github.com/geckosio/phaser3-multiplayer-game-example)

## socket&#46;io vs geckos&#46;io vs peerjs

_TODO: Note some differences here._

### When to use socket&#46;io, geckos&#46;io or peerjs?

| | socket&#46;io | geckos&#46;io | peerjs |
| --------------------------------------------------------------- | :-----------: | :-----------: | :-----: |
| Real-Time Multiplayer Game<br>(_with authoritative server_) | | &#9679; | |
| Real-Time Multiplayer Game<br>(_without authoritative server_) | | | &#9679; |
| Turn based Multiplayer Game<br>(_with authoritative server_) | &#9679; | | |
| Turn based Multiplayer Game<br>(_without authoritative server_) | &#9679; | | &#9679; |
| Chat App | &#9679; | | &#9679; |
| Any other App with Real-Time communication | &#9679; | | &#9679; |

## License

The BSD 3-Clause License (BSD-3-Clause) 2019 - [Yannick Deubel](https://github.com/yandeu). Please have a look at the [LICENSE](LICENSE) for more details.
@@ -0,0 +1,123 @@
# Geckos&#46;io Cheatsheet

## Client

```js
// import geckos.io client
import geckos from '@geckos.io/client'
/**
* start geckos client with these options
* @param {string} options.url default is `${location.protocol}//${location.hostname}`
* @param {number} options.port default is 9208
*/
const channel = geckos(options)
// once the channel is connected to the server
channel.onConnect(error => {
if (error) console.error(error.message)
// listens for a disconnection
channel.onDisconnect(() => {})
// listens for a custom event from the server
channel.on('chat message', data => {})
// emits a message to the server
channel.emit('chat message', 'Hi!')
})
```

## Server

```js
// import geckos.io server
import geckos from '@geckos.io/server'
/**
* start geckos server with these options
* @param {Object[]} options.iceServers your iceServers array
*/
io = geckos(options)
/**
* make the server listen on a port
* @param {number} port default port is 9208
*/
io.listen()
// whenever a new channel is connected
io.onConnection(channel => {
// the channel includes its id
const { id } = channel
// whenever the channel got disconnected
channel.onDisconnect(() => {})
// listen for a custom event
channel.on('chat message', data => {})
// channel joins a room
channel.join('someRoomId')
// channel leaves a room
channel.leave()
// will trigger a specific event on all channels in a
// specific room and add the senderId as a second parameter
channel.forward(channel.sroomId).emit('chat message', 'Hello!')
// listen for a forwarded message
channel.on('chat message', (data, senderId) => {
// we know that the message was forwarded if senderId is defined
if (senderId) {
// ...
} else {
// ...
}
})
// emits a message to the channel
channel.emit('chat message', 'Hello to myself!')
// emits a message to all channels, in the same room
channel.room.emit('chat message', 'Hello everyone!')
// emits a message to all channels, in the same room, except sender
channel.broadcast.emit('chat message', 'Hello friends!')
// emits a message to all channels
io.emit('chat message', 'Hello everyone!')
// emits a message to all channels in a specific room
io.room(roomId).emit('chat message', 'Hello everyone!')
})
```

**Note**: The following event names are reserved:

- `sendOverDataChannel`
- `receiveFromDataChannel`
- `disconnected`
- `disconnect`
- `connection`
- `connect`
- `error`
- `dataChannelIsOpen`
- `sendToRoom`
- `sendToAll`
- `forwardMessage`
- `broadcastMessage`
- `rawMessage`

## Raw Messages

You can send and receive `USVString`, `ArrayBuffer` and `ArrayBufferView` using rawMessages.

```js
// send a raw message
channel.raw.emit(rawMessage)
// listen for a raw message
channel.onRaw(rawMessage => {})
```

0 comments on commit a4d2e11

Please sign in to comment.
You can’t perform that action at this time.