Giter Site home page Giter Site logo

adrianfalleiro / cordova-plugin-advanced-websocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from heimgardtechnologiesas/cordova-plugin-advanced-websocket

1.0 1.0 0.0 413 KB

License: MIT License

Java 48.65% Objective-C 39.59% JavaScript 11.76%

cordova-plugin-advanced-websocket's Introduction

Cordova Plugin for using WebSockets

npm version downloads MIT Licence

WebSocket plugin that supports custom headers, self-signed certificates, periodical sending of pings (ping-pong to keep connection alive and detect sudden connection loss when no closing frame is received).

Supported Platforms

Android

OkHttp library is included as maven dependency with version 3.10.0

iOS

SocketRocket is included as CocoaPod library with version 0.5.1

To use it, you will need to install CocoaPods on your Mac:

sudo gem install cocoapods
pod setup --verbose

Installing

Cordova

$ cordova plugin add cordova-plugin-advanced-websocket

API

Methods

wsConnect

Connecto to WebSocket using options

CordovaWebsocketPlugin.wsConnect(options, receiveCallback, successCallback, failureCallback);

Parameters

  • options: Object containing WebSocket url and other properties needed for opening WebSocket:
    • url: string; Url of WebSocket you want to connect to. This is the only mandatory property in options.
    • timeout?: number; Request timeout in milliseconds. (optional, defaults to 0)
    • pingInterval?: number; Ping interval in milliseconds if you want to keep WebSocket open and detect automatically dead WebSocket when Pongs stop returning. If you set it to 0, Pings won't be sent. (optional, defaults to 0. iOS/Android only)
    • headers?: object; Object containing custom request headers you want to send when opening WebSocket. Object keys are used as Header names, and values are used as Header values. (optional. iOS/Android only)
    • acceptAllCerts?: boolean; Set this to true if you are using secure version of WebSocket (url starts with "wss://") and you want to accept all certificates regardles of their validity. Useful when your WebSocket is using self-signed certificates. (optional, defaults to false. iOS/Android only)
  • receiveCallback: Receive callback function that is invoked with every message received through WebSocket and also when WebSocket is closed.
  • successCallback: Success callback function that is invoked with successfull connect to WebSocket.
  • failureCallback: Error callback function, invoked when connecting to WebSocket failed for whatever reason.

Callbacks

All three callback functions will get one object containing property webSocketId and some other properties depending on callback. successCallback and failureCallback callbacks will also get properties code and reason. Those two callback methods will be called only once and just one of them will be called depending on success of the outcome.

receiveCallback callback will be called multiple times during lifetime of the WebSocket. It will get object that will, appart from webSocketID property, contain also property callbackMethod so we know what type of callback is received from plugin. Possible values for callbackMethod are: onMessage, onClose, onFail. If callbackMethod has value onMessage you will also get property message which is the actual received message. If callbackMethod has value onClose you will get properties code and reason or exception. If callbackMethod has value onFail you will get properties code and exception.

webSocketId is unique reference to your WebSocket which is needed for later calls to wsSend and wsClose.

Quick Example

var accessToken = "abcdefghiklmnopqrstuvwxyz";
var wsOptions = {
    url: "wss://echo.websocket.org",
    timeout: 5000,
    pingInterval: 10000,
    headers: {"Authorization": "Bearer " + accessToken},
    acceptAllCerts: false
}

CordovaWebsocketPlugin.wsConnect(wsOptions,
                function(recvEvent) {
                    console.log("Received callback from WebSocket: "+recvEvent["callbackMethod"]);
                },
                function(success) {
                    console.log("Connected to WebSocket with id: " + success.webSocketId);
                },
                function(error) {
                    console.log("Failed to connect to WebSocket: "+
                                "code: "+error["code"]+
                                ", reason: "+error["reason"]+
                                ", exception: "+error["exception"]);
                }
            );

wsSend

Send message to WebSocket using webSocketId as a reference.

CordovaWebsocketPlugin.wsSend(webSocketId, message);

Parameters

  • webSocketId: Unique reference of your WebSocket.
  • message: Message that you want to send as a string.

Quick Example

CordovaWebsocketPlugin.wsSend(webSocketId, "Hello World!");

wsClose

Close WebSocket using webSocketId as a reference, specifying closing code and reason.

CordovaWebsocketPlugin.wsClose(webSocketId, code, reason);

Parameters

  • webSocketId: Unique reference of your WebSocket.
  • code: WebSocket closing code, see RFC6455. (optional, defaults to 1000)
  • reason: WebSocket closing reason. (optional)

Quick Example

CordovaWebsocketPlugin.wsClose(webSocketId, 1000, "I'm done!");

cordova-plugin-advanced-websocket's People

Contributors

adrianfalleiro avatar chax avatar dariofilipaj avatar pliablepixels avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.