Skip to content

Commit f95ec3b

Browse files
authored
Merge pull request #1 from vbaicu/binarydata
Binarydata
2 parents 82105ad + ee86e8b commit f95ec3b

File tree

6 files changed

+106
-11
lines changed

6 files changed

+106
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.DS_Store
3+
.vscode

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,31 @@ conn.on('eventName', data => {
4242
console.log('Received event: ', data.eventName, 'with payload: ', data.payload)
4343
})
4444
```
45+
46+
**Listener for all events**
47+
48+
```javascript
49+
50+
All events will be sent to `message` listener even if there is another listener sent for those events.
51+
52+
conn.on('message', event => {
53+
console.log("onMessage received event: ",event)
54+
})
55+
```
56+
4557
**Send Events**
4658

47-
Only supports sending serialisable objects and strings now.
48-
Binary data support is in progress.
4959

5060
```javascript
5161
conn.send('eventName',{x:0,y:0})
5262
```
5363

64+
**Send Data**
65+
66+
```javascript
67+
conn.sendData('eventName',new Uint8Array())
68+
```
69+
5470
Client for browser is build on top of WebSocket javascript object. TFPSocketsClient provides access to base socket object via `ws` property and can also be initialised with a pre initialised WebSocket instance using:
5571
```javascript
5672
var client = new TFPSocketsClient(null, null,initialisedWS)

TFPSocketsClient-Browser.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
const isString = s => typeof (s) === 'string' || s instanceof String;
22

3+
const TFPUtils_packData = (data, eventName) => {
4+
if(eventName.length > 255) {
5+
throw "Event name length should be < 255"
6+
return
7+
}
8+
let enc = new TextEncoder("utf-8");
9+
eventName = enc.encode(eventName)
10+
let header = new Uint8Array([eventName.length, ...eventName])
11+
return new Uint8Array([...header,...data])
12+
}
13+
14+
const TFPUtils_unpackData = unpacked => {
15+
let headerSize = new Uint8Array(unpacked.slice(0,1))[0]
16+
let header = new Uint8Array(unpacked.slice(1,headerSize+1))
17+
let data = new Uint8Array(unpacked.slice(headerSize+1))
18+
let eventName = new TextDecoder('utf-8').decode(header)
19+
return {eventName,payload:data}
20+
}
21+
322
class TFPSocketsClient {
423

524
constructor(url, protocols,cutomws = null) {
@@ -42,17 +61,18 @@ class TFPSocketsClient {
4261
}
4362
this.ws.binaryType = "arraybuffer"
4463
this.ws.onmessage = (data) => {
64+
var processedEvent;
4565
if (isString(data.data)) {
46-
//handle incomming message
4766
try {
48-
let obj = JSON.parse(data.data);
49-
this.EventStream.emit(obj.eventName, obj)
67+
processedEvent = JSON.parse(data.data);
5068
} catch (e) {
5169
console.log("Message doesn't repect protocol with exception: ", e)
5270
}
5371
} else {
54-
//binary data wip
72+
processedEvent = TFPUtils_unpackData(data.data)
5573
}
74+
this.EventStream.emit(processedEvent.eventName, processedEvent)
75+
this.EventStream.emit('message',processedEvent)
5676
}
5777
}
5878

@@ -69,6 +89,14 @@ class TFPSocketsClient {
6989
}
7090
}
7191

92+
sendData(event,payload) {
93+
if (this.isOpen) {
94+
this.ws.send(TFPUtils_packData(payload,event))
95+
} else {
96+
console.log("Connection is not opened")
97+
}
98+
}
99+
72100
}
73101

74102
if (typeof module !== 'undefined' && module.exports) {

example.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,45 @@ const express = require('express');
22
const http = require('http');
33
const url = require('url');
44
const TFPSockets = require('./index')
5+
const TextEncoder = require('text-encoding').TextEncoder
6+
const TextDecoder = require('text-encoding').TextDecoder
57

68

79
const app = express();
810

911

1012
const server = http.createServer(app);
1113

14+
const eventFilter = (event,f) => [event].filter(f);
15+
1216
const TFPServer = new TFPSockets.server(server,["chat"])
1317
TFPServer.on('connection',client => {
1418
client.on('testEvent', data => {
1519
console.log("test event payload: ", data.payload)
1620
client.send('testEventBack',{x:1,y:100})
1721
})
22+
23+
client.on('message',event => {
24+
console.log("onMessage received event: ",event)
25+
})
26+
27+
client.on('dataEvent', event => {
28+
let text = new TextDecoder('utf-8').decode(event.payload)
29+
console.log("received data event with payload ", text)
30+
})
31+
1832
})
1933

2034

2135
const testClient = new TFPSockets.client("ws://localhost:1507",["chat"])
2236
testClient.on('open', event => {
2337
console.log("test client open")
2438
testClient.send('testEvent',{x:10,y:89})
39+
testClient.send('event2',{})
40+
41+
let data = new TextEncoder('utf-8').encode("Hello world as Uint8Array")
42+
testClient.sendData('dataEvent',data);
43+
2544
})
2645

2746
testClient.on('testEventBack', data => {

lib/TFPSockets.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
const WebSocket = require('ws')
2+
const TextEncoder = require('text-encoding').TextEncoder
3+
const TextDecoder = require('text-encoding').TextDecoder
24

35
const isString = s => typeof (s) === 'string' || s instanceof String;
46

7+
const TFPUtils_packData = (data, eventName) => {
8+
if(eventName.length > 255) {
9+
throw "Event name length should be < 255"
10+
return
11+
}
12+
let enc = new TextEncoder("utf-8");
13+
eventName = enc.encode(eventName)
14+
let header = new Uint8Array([eventName.length, ...eventName])
15+
return new Uint8Array([...header,...data])
16+
}
17+
18+
const TFPUtils_unpackData = unpacked => {
19+
let headerSize = new Uint8Array(unpacked.slice(0,1))[0]
20+
let header = new Uint8Array(unpacked.slice(1,headerSize+1))
21+
let data = new Uint8Array(unpacked.slice(headerSize+1))
22+
let eventName = new TextDecoder('utf-8').decode(header)
23+
return {eventName,payload:data}
24+
}
25+
526
class TFPBaseEventStream {
627
constructor() {
728
this.EventStream = (function () {
@@ -49,17 +70,18 @@ class TFPSocketsClient extends TFPBaseEventStream {
4970
this.EventStream.emit('open',event)
5071
})
5172
this.ws.on('message', (data) => {
73+
var processedEvent;
5274
if (isString(data)) {
53-
//handle incomming message
5475
try {
55-
let obj = JSON.parse(data);
56-
this.EventStream.emit(obj.eventName, obj)
76+
processedEvent = JSON.parse(data);
5777
} catch (e) {
5878
console.log("Message doesn't repect protocol with exception: ", e)
5979
}
6080
} else {
61-
//binary data wip
81+
processedEvent = TFPUtils_unpackData(data)
6282
}
83+
this.EventStream.emit(processedEvent.eventName, processedEvent)
84+
this.EventStream.emit('message',processedEvent)
6385
})
6486
this.ws.on('close', () => this.EventStream.emit('close') )
6587
}
@@ -73,6 +95,14 @@ class TFPSocketsClient extends TFPBaseEventStream {
7395
}
7496
}
7597

98+
sendData(event,payload) {
99+
if (this.isOpen) {
100+
this.ws.send(TFPUtils_packData(payload,event))
101+
} else {
102+
console.log("Connection is not opened")
103+
}
104+
}
105+
76106
}
77107

78108
class TFPSocketsServer extends TFPBaseEventStream {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tfpsockets",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Simple event based websockets wrapper",
55
"main": "index.js",
66
"scripts": {
@@ -19,6 +19,7 @@
1919
"license": "MIT",
2020
"dependencies": {
2121
"express": "^4.16.0",
22+
"text-encoding": "^0.6.4",
2223
"ws": "^3.2.0"
2324
}
2425
}

0 commit comments

Comments
 (0)