On the server:
var RelayServer = require('websocket-relay');
var relayServer = new RelayServer({ port: 4000 });
var client1 = relayServer.registerClient();
var client2 = relayServer.registerClient();
console.log(client1.id, client1.token); // => 0 11ac27cd0c17bd1c7ba9aa4285979aea
console.log(client2.id, client2.token); // => 1 7c2e2bc62cb06d7b90651cd4a2f369fb
relayServer.registerRelayChannel(client1.id, client2.id);
In the first browser client:
<script type="text/javascript" src="websocket_relay.min.js"></script>
<script type="text/javascript">
var relay = new WebSocketRelay('localhost:4000', {
clientId: 0,
token: '11ac27cd0c17bd1c7ba9aa4285979aea',
});
var channel = relay.createChannel(1);
</script>
In the second browser client:
<script type="text/javascript" src="websocket_relay.min.js"></script>
<script type="text/javascript">
var relay = new WebSocketRelay('localhost:4000', {
clientId: 1,
token: '7c2e2bc62cb06d7b90651cd4a2f369fb'
});
var channel = relay.createChannel(0);
</script>
Now you can freely send and receive data through the channel to the other browser client:
<script type="text/javascript">
channel.send('hello world');
channel.on('message', function(message) {
console.log(message);
});
</script>
This class represents a WebSocket relay server. It is an EventEmitter
.
options
Objectport
NumberauthorizeAllChannels
Boolean
callback
Function
Construct a new server object.
Whether to allow relay channels between clients without registration. Default false
.
Close the server and terminate all clients, calls callback when done with an error if one occured. Must be called after the RelayServer constructor calls its callback.
function (error) { }
If the underlying server emits an error, it will be forwarded here. If there is no listener, it will throw the error.
This class represents a connection to a WebSocket relay server. It is an EventEmitter
.
address
Stringauthentication
ObjectclientId
Numbertoken
String
callback
Function
address
is the host and port of the relay server. When it connects with the relay server callback
is called.
ID of the client returned from registration on the server.
Client's authentication token.
targetClientId
Number
Returns an instance of RelayChannel
that represents a bidirectional channel with the target client.
Represents a bidirectional channel with a target client. It is an EventEmitter
.
message
String
Sends the message to other client. If the other client has not connected the relay server yet, the message will be queued. Only string messages are supported.
Emits each queued message (the messages were received before the channel was created).
function (message) { }
The client received a relay message
function () { }
The client formed a connection with the relay server (channel.send()
can be called).
npm test
Try upgrading node if you receive syntax errors.
npm compile