rsocket-events-server
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -6,6 +6,15 @@ 'use strict'; | ||
/** | ||
* EventsServer | ||
* Waiting for client to initiate connection. | ||
* | ||
* successful connection message contain a port message. | ||
* server will use the port message to return confirmation for the connection. | ||
*/ | ||
class EventsServer { | ||
constructor(option) { | ||
this.debug = false; | ||
this.eventType = option.eventType || 'defaultEventsListener'; | ||
this.address = option.address; | ||
this.debug = option.debug; | ||
this._getEventData = option.processEvent || | ||
@@ -33,32 +42,21 @@ (data => data.type === this.eventType ? data.detail : null); | ||
this._clientChannelPort = ev.ports[0]; | ||
} else { | ||
return; | ||
} | ||
this._clientChannelPort.postMessage({type: 'connect'}); | ||
this._clientChannelPort.postMessage({type: 'connect'}); | ||
this._listeners = (0, _rsocketEventsClient.updateListeners)({ | ||
func: connectionHandler, | ||
listeners: this._listeners, | ||
type: 'message', | ||
}); | ||
const connectionHandler = ev => { | ||
const event = (0, _rsocketEventsClient.getMessageData)(ev); | ||
switch (event.type) { | ||
case 'close': { | ||
this.onStop(); | ||
console.log('server close'); | ||
} | ||
} | ||
}; | ||
this._listeners = (0, _rsocketEventsClient.updateListeners)({ | ||
func: connectionHandler, | ||
listeners: this._listeners, | ||
type: 'message', | ||
}); | ||
this._clientChannelPort.addEventListener('message', connectionHandler); | ||
this._clientChannelPort.start(); | ||
this._onConnection( | ||
new EventsClient({ | ||
clientChannelPort: this._clientChannelPort, | ||
listeners: this._listeners, | ||
}) | ||
); | ||
this._clientChannelPort.addEventListener('message', ev => | ||
connectionHandler(ev, this.onStop.bind(this))); | ||
this._clientChannelPort.start(); | ||
this._onConnection( | ||
new ServerChannel({ | ||
clientChannelPort: this._clientChannelPort || new MessagePort(), | ||
listeners: this._listeners, | ||
debug: this.debug, | ||
}) | ||
); | ||
} | ||
} | ||
@@ -71,5 +69,5 @@ | ||
onStop() { | ||
this._clientChannelPort.postMessage({type: 'disconnect'}); | ||
this._clientChannelPort.close(); | ||
console.log('server onStop'); | ||
this._clientChannelPort && | ||
this._clientChannelPort.postMessage({type: 'disconnect'}); | ||
this._clientChannelPort && this._clientChannelPort.close(); | ||
} | ||
@@ -79,6 +77,12 @@ } | ||
class EventsClient { | ||
constructor(options) { | ||
this.clientChannelPort = options.clientChannelPort; | ||
this._listeners = options.listeners || []; | ||
/** | ||
* ServerChannel implements IChannelServer | ||
* | ||
* server connection implementation | ||
*/ | ||
class ServerChannel { | ||
constructor({clientChannelPort, listeners, debug}) { | ||
this.clientChannelPort = clientChannelPort; | ||
this._listeners = listeners || []; | ||
this.debug = debug || false; | ||
} | ||
@@ -89,3 +93,2 @@ | ||
disconnect: () => { | ||
console.log('server disconnect'); | ||
this.clientChannelPort.postMessage( | ||
@@ -101,11 +104,2 @@ (0, _rsocketEventsClient.newMessage)({ | ||
receive: cb => { | ||
const requestMessage = eventMsg => { | ||
const {payload, type} = (0, _rsocketEventsClient.getMessageData)( | ||
eventMsg | ||
); | ||
if (type === 'request') { | ||
console.log('receive-request', payload); | ||
cb(payload); | ||
} | ||
}; | ||
this._listeners = (0, _rsocketEventsClient.updateListeners)({ | ||
@@ -117,5 +111,9 @@ func: requestMessage, | ||
this.clientChannelPort.addEventListener('message', requestMessage); | ||
this.clientChannelPort.addEventListener('message', eventMsg => | ||
requestMessage(eventMsg, cb, this.debug)); | ||
}, | ||
send: msg => { | ||
if (this.debug) { | ||
console.log(`Server responses with payload: ${JSON.stringify(msg)}`); | ||
} | ||
this.clientChannelPort.postMessage( | ||
@@ -131,1 +129,20 @@ (0, _rsocketEventsClient.newMessage)({ | ||
} | ||
const requestMessage = (eventMsg, cb, debug) => { | ||
const {payload, type} = (0, _rsocketEventsClient.getMessageData)(eventMsg); | ||
if (type === 'request') { | ||
if (debug) { | ||
console.log(`Server receive request with payload: ${payload}`); | ||
} | ||
cb(payload); | ||
} | ||
}; | ||
const connectionHandler = (ev, onStop) => { | ||
const event = (0, _rsocketEventsClient.getMessageData)(ev); | ||
switch (event.type) { | ||
case 'close': { | ||
onStop(); | ||
} | ||
} | ||
}; |
@@ -1,15 +0,7 @@ | ||
/** Copyright (c) Facebook, Inc. and its affiliates. | ||
/** | ||
* written with <3 by scaleCube-js maintainers | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the 'License'); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* RSocketEventsServer Transport provider for event base messages | ||
* browser <--> browser | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an 'AS IS' BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
@@ -55,5 +47,13 @@ */ | ||
}); | ||
this._subscribers.add(eventClientConnection); | ||
eventClientConnection.connect(); | ||
subscriber.onNext(eventClientConnection); | ||
if (eventClientConnection) { | ||
this._subscribers.add(eventClientConnection); | ||
eventClientConnection.connect(); | ||
subscriber.onNext(eventClientConnection); | ||
} else { | ||
subscriber.onError( | ||
new Error( | ||
`unable to create connection - address: ${this.address}` | ||
) | ||
); | ||
} | ||
}); | ||
@@ -60,0 +60,0 @@ }, |
{ | ||
"name": "rsocket-events-server", | ||
"description": "RSocket WebSocket server", | ||
"version": "0.0.14", | ||
"description": "RSocket Events server for use in browser environments", | ||
"version": "0.0.15", | ||
"repository": { | ||
@@ -14,3 +14,3 @@ "type": "git", | ||
"rsocket-core": "^0.0.10", | ||
"rsocket-events-client": "^0.0.14", | ||
"rsocket-events-client": "^0.0.15", | ||
"rsocket-flowable": "^0.0.10", | ||
@@ -17,0 +17,0 @@ "rsocket-types": "^0.0.10" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15409
217
9
+ Addedrsocket-events-client@0.0.15(transitive)
- Removedrsocket-events-client@0.0.14(transitive)