Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/drulac/uws-with-on

uws superset who allow you to listen on event like with socket.io
https://github.com/drulac/uws-with-on

Last synced: about 14 hours ago
JSON representation

uws superset who allow you to listen on event like with socket.io

Awesome Lists containing this project

README

        

# uws-with-on
uws superset who allow you to listen on event like with socket.io
(the code source work both in the browser and in NodeJS)

use is really easy :
(these sample uses the [socket.io-with-get.js](https://github.com/Drulac/socket.io-with-GET) superset too)

you can use it in the client :
```html

.


var socket = new WebSocket("ws://localhost:5000");

socket.onopen = async function (event) {
socket = new SocketWithOn(socket);
socket = new Socket(socket);

socket.on('name', async (data)=>{
return "My name is Jhon... Jhon Doe...";
});

let data = await socket.get("ping", {start: new Date().getTime()}).catch(err=>{throw new Error(err)});
let pingTime = new Date().getTime() - data.start;

console.log(pingTime+" ms");
};

```

and this on the server :
```js
const WebSocketServer = require('uws').Server;
const SocketWithOn = require('uws-with-on.js');
const Socket = require('socket.io-with-get');
const wss = new WebSocketServer({ port: 5000 });

wss.on('connection', async function(socket) {
try{
socket = new SocketWithOn(socket);
socket = new Socket(socket);

socket.on('ping',async (data)=>{
return data;
});

let data = await socket.get("name", {}).catch(err=>{throw new Error(err)});
console.log(data);
}catch(e){
console.log(e);
}
});