Skip to content

Client Connection_0.2.1

bristleback edited this page May 19, 2013 · 1 revision

As said in previous chapter, the main component for connection handling is Bristleback.Client class. To create new instance of Client class, factory method should be used:

    var client = Bristleback.newClient(config);

Configuration object can be defined as any normal JavaScript object, for example:

    var config = {
      //  serverUrl: "ws://localhost:8765/websocket",
      OnOpen: function(event) {
        $('#status').text("The WebSocket Connection Is Open.");
      },
      OnClose: function(event) {
        $('#status').text("The WebSocket Connection Is Closed.");
      }
    };

Client object uses several configuration properties. The list below shows all of them with default values (if such configuration property is not passed by the user):

Property: serverUrl: String
Default value: Value of Bristleback.LOCAL_HOSTNAME
Description: Hostname of the server. Value of this property must fallow WebSocket interface constructor rules (http://dev.w3.org/html5/websockets/#the-websocket-interface)
Property: serializationEngine: String
Default value: "json"
Description: Serialization engine is used to serialize and deserialize WebSockets messages, they are located in Bristleback.serialization.serializationEngines map. Value of this parameter should be the key of the serializationEngines map entry. By default, Bristleback.serialization.JsonEngine serialization engine is used.
Property: dataController: String
Default value: "system.controller.action"
Description: Client data controller serves processes incoming messages and may serve as an additional layer in sending messages to the server. Data controllers are stored in Bristleback.controller.controllers map. Value of this parameter should be the key of the controllers map entry. By default, Bristleback.controller.ActionController is used.
Property: timeout: Number
Default value: "360000"
Description: Actually not supported
Property: OnOpen: Function
Default value:
    function() {
      Bristleback.Console.log("Connected to " + config.serverUrl);
      alert("connected");
    };
Description: This function is called by the WebSocket object when connection is established.
Property: OnClose: Function
Default value:
    function() {
      Bristleback.Console.log("Disconnected from " + config.serverUrl);
      alert("disconnected");
    };
Description: This function is called by the WebSocket object when connection is closed.

After creating an instance of Client class, we can immedietely connect to the server by invoking client.connect() method. To check actual connection state, invoke client.getConnectionState() method. There are also two more specific methods: isConnected() and isDisconnected(). To close connection, run client.disconnect().