new Client(settings)
Eureca client class
This constructor takes an optional settings object
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
settings | object | optional | have the following properties |
Properties:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
settings.uri | URI | Eureca server WS uri, browser client can automatically guess the server URI if you are using a single Eureca server but Nodejs client need this parameter. |
||
settings.prefix | string | optional | eureca.io | This determines the websocket path, it's unvisible to the user but if for some reason you want to rename this path use this parameter. |
settings.retry | int | optional | 20 | Determines max retries to reconnect to the server if the connection is lost. |
settings.autoConnect | boolean | optional | true | Estabilish connection automatically after instantiation. |
Examples
//Example of a nodejs client
var Eureca = require('eureca.io');
var client = new Eureca.Client({ uri: 'ws://localhost:8000/', prefix: 'eureca.io', retry: 3 });
client.ready(function (serverProxy) {
// ...
});
//Equivalent browser client
<!doctype html>
<html>
<head>
<script src="/eureca.js"></script>
</head>
<body>
<script>
var client = new Eureca.Client({prefix: 'eureca.io', retry: 3 });
//uri is optional in browser client
client.ready(function (serverProxy) {
// ...
});
</script>
</body>
</html>
Namespaces
Members
-
serverProxy :object
-
When the connection is estabilished, the server proxy object allow calling exported server functions.
Methods
-
authenticate()
-
Send authentication request to the server.
this can take an arbitrary number of arguments depending on what you defined in the server side
when the server receive an auth request it'll handle it and return null on auth success, or an error message if something goes wrong
you need to listed to auth result throught authResponse event
Important : it's up to you to define the authenticationmethod in the server sideExample
var client = new Eureca.Client({..}); //listen to auth response client.authResponse(function(result) { if (result == null) { // ... Auth OK } else { // ... Auth failed } }); client.ready(function(){ //send auth request client.authenticate('your_auth_token'); });
-
connect()
-
connect client
-
disconnect()
-
close client connection
-
donDisconnect()
-
Bind a callback to 'disconnect' event @see Client disconnect event
Note : you can also use Client.on('disconnect', callback) to bind disconnect event
-
isReady() → {boolean}
-
indicate if the client is ready or not, it's better to use client.ready() event, but if for some reason
you need to check if the client is ready without using the event system you can use this.Returns:
boolean- true if the client is ready
Example
var client = new Eureca.Client({..}); //... if (client.isReady()) { client.serverProxy.foo(); }
-
onAuthResponse()
-
Bind a callback to 'authResponse' event @see Client authResponse event
Note : you can also use Client.on('authResponse', callback) to bind authResponse event
-
onConnect()
-
Bind a callback to 'connect' event
Note : you can also use Client.on('connect', callback) to bind connect event
-
onConnectionLost()
-
Bind a callback to 'connectionLost' event
Note : you can also use Client.on('connectionLost', callback) to bind connectionLost event
-
onConnectionRetry()
-
Bind a callback to 'connectionRetry' event
Note : you can also use Client.on('connectionRetry', callback) to bind connectionRetry event
-
onError()
-
Bind a callback to 'error' event @see Client error event
Note : you can also use Client.on('error', callback) to bind error event
-
onMessage()
-
Bind a callback to 'message' event @see Client message event
Note : you can also use Client.on('message', callback) to bind message event
-
onUnhandledMessage()
-
Bind a callback to 'unhandledMessage' event @see Client unhandledMessage event
Note : you can also use Client.on('message', callback) to bind unhandledMessage event
-
ready()
-
Bind a callback to 'ready' event @see Client ready event
Note : you can also use Client.on('ready', callback) to bind ready event
-
send(rawData)
-
Send user data to the server
Parameters:
Name Type Description rawData any data to send (must be serializable type)
-
update()
-
Bind a callback to 'update' event @see Client update event
Note : you can also use Client.on('update', callback) to bind update event
Events
-
authResponse
-
Triggered when the client receive authentication response from the server.
The server should return a null response on authentication success. -
connectionRetry
-
triggered when the connection is lost and the client try to reconnect.
-
disconnect
-
triggered when the connection is lost after all retries to reestabilish it.
-
error
-
triggered if an error occure.
Properties:
Name Type Description error String the error message
-
message
-
Triggered when the client receive a message from the server.
This event can be used to intercept exchanged messages betweens client and server, if you need to access low level network messages -
ready
-
Triggered when the connection is estabilished and server remote functions available to the client.
Properties:
Name Type Description serverProxy Proxy server proxy object.
Example
client.ready(function (serverProxy) { serverProxy.hello(); });
-
unhandledMessage
-
Triggered when the client receive a message from the server and is not able to handle it.
this mean that the message is not an internal eureca.io message.
if for some reason you need to exchange send/receive raw or custom data, listen to this event which in countrary to message event will only trigger for non-eureca messages.