socketConnection


Source link: https://github.com/nihasKalam07/socketConnections

Its a websocket client library for Android

Version 1.0.0

Integration

The lib is available on jcenter, you can find it with

dependencies {

  compile 'com.nihaskalam.android:socket-connections:1.0.0' 
}
 

API Overview

Here's the API in a nutshell.

// Create a new QSocketOptions instance and a QSocket instance

QSocketOptions options = new QSocketOptions().setAuthorizationToken("token");
 QSocket qsocket = new QSocket(options, context);
 

//connect to socket

qSocket.connect(new ConnectionEventListener() {

 @Override

public void onConnectionStateChange(ConnectionStateChange change) {

 Log.i("WebsocketConnection", String.format("[%d] Connection state changed from [%s] to [%s]", timestamp(),

 change.getPreviousState(), change.getCurrentState()));

}

 @Override

public void onError(String message, String code, Exception e) {

 Log.i("WebsocketConnection", String.format("[%d] An error was received with message [%s], code [%s], exception [%s]",

 timestamp(), message, code, e));

}

  
}
, ConnectionState.ALL);
 
}
 

Implement the ConnectionEventListener interface to receive connection state change events: available events are CONNECTING, CONNECTED, DISCONNECTING, DISCONNECTED, ALL. Connection state changes will be available in onConnectionStateChange method. If there is any error when trying to connect onError metjod will be called.

// Subscribe to a channel

QSocket uses the concept of channels as a way of subscribing to data. They are identified and subscribed to by a simple name. As mentioned above, channel subscriptions need only be registered once per QSocket instance. They are preserved across disconnection and re-established with the server on reconnect. They should NOT be re-registered.

Channel channel = = qSocket.subscribe("my-channel", new ChannelEventListener() {

 @Override

public void onSubscriptionSucceeded(String channelName) {

 Log.i("channelSubscription", String.format("[%d] Subscription to channel [%s] succeeded", timestamp(), channelName));

}

 @Override

public void onEvent(String channelName, String eventName, String data) {

 Log.i("ReceivedEventData:", String.format("[%d] Received event [%s] on channel [%s] with data [%s]", timestamp(),

eventName, channelName, data));

}

  
}
);
 

Here "my-channel" is the channel name is to subscribe. ChannelEventListener is used to get both rotocol related events such as subscription succeeds(will be received in onSubscriptionSucceeded method) and data events triggered to that subscribed channel(will be received in onEvent method).

// Disconnect from the service (or become disconnected my network conditions)

qSocket.disconnect();
 

// Reconnect, with all channel subscriptions and event bindings automatically recreated

qSocket.connect();
 

// The state change listener is notified when the connection has been re-established, // the subscription to "my-channel" still exist. More information in reference format can be found below.

The QSocket constructor

The standard constructor take an QSocketOptions instance and current context. To add Authorization, you can put AuthorizationToken in QSocketOptions

QSocketOptions options = new QSocketOptions().setAuthorizationToken("1234567890");
 QSocket qSocket = new QSocket(options, context);
 

If you need finer control over the endpoint then the setHost, setWsPort and setWssPort methods can be employed.

Connecting

In order to send and receive messages you need to connect to QSocket.

QSocket qSocket = new QSocket(options, context);
 qSocket.connect();
 

Reconnecting

The connect method is also used to re-connect in case the connection has been lost, for example if an Android device loses reception. Note that the state of channel subscriptions will be preserved while disconnected and re-negotiated with the server once a connection is re-established.

Disconnecting

qSocket.disconnect();
 

After disconnection the QSocket instance will release any internally allocated resources (threads and network connections)

Example Android application using SocketManager library:

public class MainActivity extends AppCompatActivity {

  private QSocket qSocket;
  private final long startTime = System.currentTimeMillis();

  private Channel channel;
  private TextView dataTV;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

dataTV = (TextView) findViewById(R.id.dataTV);

QSocketOptions options = new QSocketOptions().setAuthorizationToken("1234567890").setEncrypted(false);

qSocket = new QSocket(options, this);

  
}

 public void connect(View view) {

qSocket.connect(new ConnectionEventListener() {

 @Override

 public void onConnectionStateChange(ConnectionStateChange change) {

  Log.i("WebsocketConnection", String.format("[%d] Connection state changed from [%s] to [%s]", timestamp(),

 change.getPreviousState(), change.getCurrentState()));

  doDisplay(String.format("[%d] Connection state changed from [%s] to [%s]", timestamp(),

 change.getPreviousState(), change.getCurrentState()));

 
}

  @Override

 public void onError(String message, String code, Exception e) {

  Log.i("WebsocketConnection", String.format("[%d] An error was received with message [%s], code [%s], exception [%s]",

 timestamp(), message, code, e));

  doDisplay(String.format("[%d] An error was received with message [%s], code [%s], exception [%s]",

 timestamp(), message, code, e));

 
}

}
, ConnectionState.ALL);

  
}

public void disconnect(View view) {

qSocket.disconnect();

  
}

public void subscribeChannel(View view) {

channel = qSocket.subscribe("Channel B", new ChannelEventListener() {

 @Override

 public void onSubscriptionSucceeded(String channelName) {

  Log.i("channelSubscription", String.format("[%d] Subscription to channel [%s] succeeded", timestamp(), channelName));

  doDisplay(String.format("[%d] Subscription to channel [%s] succeeded", timestamp(), channelName));

 
}

  @Override

 public void onEvent(String channelName, String eventName, String data) {

  Log.i("ReceivedEventData:", String.format("[%d] Received event [%s] on channel [%s] with data [%s]", timestamp(),

 eventName, channelName, data));

  doDisplay(String.format("[%d] Received event [%s] on channel [%s] with data [%s]", timestamp(),

 eventName, channelName, data));

 
}

}
);

  
}

public void unsubscribeChannel(View view) {

qSocket.unsubscribe("Channel B", new ChannelUnsubscriptionEventListener() {

 @Override

 public void onUnsubscribed(String channelName) {

  Log.i("channelUnsubscription", String.format("[%d] Unsubscription to channel [%s] succeeded", timestamp(), channelName));

  doDisplay(String.format("[%d] Unsubscription to channel [%s] succeeded", timestamp(), channelName));

 
}

}
);

  
}

private long timestamp() {

return System.currentTimeMillis() - startTime;
  
}

private void doDisplay(final String data) {

runOnUiThread(new Runnable() {

 @Override

 public void run() {

dataTV.setText(data);

 
}

}
);

  
}
 
}
 

Note;- Also you should include ' http://clojars.org/repo' in Default Library repositiry settings in Android studio to include library. This is because org.java-websocket:java-websocket:1.3.1 library that is used for websocket integration is hosted in clojars.

Licence

Copyright 2016 Nihas Kalam  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

One recycler view adapter for all your project.

Android Youtube Player library without any dependency, webview based.

This project contains RxJava 2 implementations of the sample operators found in the RxMarbles Android Application.

Simple custom ViewGroup with two shapes inside and simple scale animation. You can flexible customizing it. (For example separating shape into two buttons or into single).

Want to detect human faces on a camera preview stream in real time? Well, you came to the right place.

FaceDetector is a library which:

  • detects faces
  • works on Android
  • very simple to use
  • comes with Fotoapparat integration (but you can use it with whatever you like)
  • with a C++ core which can easily be ported to iOS (we have plans for that)

A cute day night switch for android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes