https://github.com/jcaguilera/nativescript-betterwebsockets
  
  
    A Client and Server WebSockets plugin for Nativescript on Android. 
    https://github.com/jcaguilera/nativescript-betterwebsockets
  
android nativescript websocket-client websocket-server websockets
        Last synced: 2 months ago 
        JSON representation
    
A Client and Server WebSockets plugin for Nativescript on Android.
- Host: GitHub
- URL: https://github.com/jcaguilera/nativescript-betterwebsockets
- Owner: JCAguilera
- License: mit
- Created: 2018-09-15T19:28:04.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-18T19:57:34.000Z (about 7 years ago)
- Last Synced: 2024-12-20T13:22:38.524Z (11 months ago)
- Topics: android, nativescript, websocket-client, websocket-server, websockets
- Language: TypeScript
- Homepage:
- Size: 2.54 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
Awesome Lists containing this project
README
          
#  BetterWebsockets for Nativescript
[Java WebSockets](https://github.com/TooTallNate/Java-WebSocket) Library implementation for Nativescript. Allows you to create a WebSocket Client and Server on Android.
[](https://www.paypal.me/JCAguilera) [](https://juankyapps.com/donate) [](https://juankyapps.com/donate)
## Requirements
  
This plugin works on Android only!
## Installation
```javascript
tns plugin add nativescript-betterwebsockets
```
## Usage
```javascript
import { WebSocketClient, WebSocketServer } from 'nativescript-betterwebsockets';  
```
**WebSocketClient:**
```javascript
/** 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();
```
```javascript
// 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](https://static.javadoc.io/org.java-websocket/Java-WebSocket/1.3.9/org/java_websocket/client/WebSocketClient.html).
**WebSocketServer:**
```javascript
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](https://static.javadoc.io/org.java-websocket/Java-WebSocket/1.3.9/org/java_websocket/server/WebSocketServer.html).
## License
MIT License - see [LICENSE](LICENSE) for more details.