Skip to content

JCAguilera/nativescript-betterwebsockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BetterWebsockets for Nativescript

Java WebSockets Library implementation for Nativescript. Allows you to create a WebSocket Client and Server on Android.

PayPal Donation Bitcoin Donation Other Donation Methods

Requirements

This plugin works on Android only!

Installation

tns plugin add nativescript-betterwebsockets

Usage

import { WebSocketClient, WebSocketServer } from 'nativescript-betterwebsockets';  

WebSocketClient:

/** Inside your page component **/
let url = 'wss://echo.websocket.org';
this.client = new WebSocketClient(url);
// Open Event
this.client.on('open', function(handshake){
    console.log('Opened!');
    this.main(); // Do everything else here
});
// Message Event
this.client.on('message', function(message){ 
    console.log('New message: ' + message);
});
// Close Event
this.client.on('close', function(code, reason, remote){
    console.log('Closed!');
});
// Error Event
this.client.on('error', function(ex){
    console.log('Error!');
});
// Connect (Do after setting the events!).
this.client.connect();
// This will be executed after the open event.
main() {
    // To prevent crashes, send messages after the socket is opened.
    this.client.send('Hey!');
    this.client.close(); // Close the client :(
}

Other WebSocketClient methods are avaliable here.

WebSocketServer:

let port = 3000;
this.server  =  new  WebSocketServer(port);
// Start Event (Server Started successfully)
this.server.on('start', () => {
    console.log('Server listening on *:'  +  port);
});
// Open Event (Someone connects)
this.server.on('open', (conn, handshake) => {
    console.log('New Conenction!');
    conn.send('You are connected!'); // Send message to socket
});
// Message Event
this.server.on('message', (conn, message) => {
    console.log('New message: ' + message);
});
// Close event
this.server.on('close', (conn, code, reason, remote) => {
    console.log('Someone left!');
});
// Error event
this.server.on('error', (conn, ex) => {
    console.log('Error!');
});
// Starts the server
this.server.start();

Other WebSocketServer methods are avaliable here.

License

MIT License - see LICENSE for more details.

About

A Client and Server WebSockets plugin for Nativescript on Android.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published