2 * LwsService.java - libwebsockets test service for Android
4 * Copyright (C) 2016 Alexander Bruines <alexander.bruines@gmail.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
21 package org.libwebsockets.client;
23 import android.os.Message;
24 import android.os.RemoteException;
25 import android.util.Log;
27 public class LwsService extends ThreadService {
30 * Commands that can be send to this service
32 public final static int MSG_SET_CONNECTION_PARAMETERS = 1;
35 * Messages that may be send to output Messenger
36 * Clients should handle these messages.
38 public final static int MSG_DUMB_INCREMENT_PROTOCOL_COUNTER = 1;
39 public final static int MSG_LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 2;
40 public final static int MSG_LWS_CALLBACK_CLIENT_ESTABLISHED = 3;
42 public static class ConnectionParameters {
50 this.serverAddress = serverAddress;
51 this.serverPort = serverPort;
56 * Handle incoming messages from clients of this service
59 public void handleInputMessage(Message msg) {
62 case MSG_SET_CONNECTION_PARAMETERS: {
63 LwsService.ConnectionParameters parameters = (ConnectionParameters) msg.obj;
64 setConnectionParameters(
65 parameters.serverAddress,
71 super.handleInputMessage(msg);
77 * The run() function for the thread.
78 * For this test we implement a very long lived task
79 * that sends many messages back to the client.
81 public void workerThreadRun() {
88 // service the websockets
91 // Check if we must quit or suspend
92 synchronized (mThreadLock){
94 // We are asked to suspend the thread
98 } catch (InterruptedException e) {}
101 // The signal to quit was given
106 // Throttle the loop so that it iterates once every 50ms
110 catch (InterruptedException e) {
118 /** Load the native libwebsockets code */
121 System.loadLibrary("lwsservice");
123 catch(UnsatisfiedLinkError ule) {
124 Log.e("LwsService", "Warning: Could not load native library: " + ule.getMessage());
127 public native boolean initLws();
128 public native void exitLws();
129 public native void serviceLws();
130 public native void setConnectionParameters(String serverAddress, int serverPort);
131 public native boolean connectLws();