Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shannah/cn1-websockets

Websockets library for Codename One
https://github.com/shannah/cn1-websockets

Last synced: about 1 month ago
JSON representation

Websockets library for Codename One

Awesome Lists containing this project

README

        

= Codename One WebSockets Library

This library adds WebSocket client support for Codename One apps.

== Supported Platforms

. JavaSE (Codename One Simulator)
. Android
. iOS
. Javascript
. Windows (UWP)

== Installation

**For Maven Projects:**
To use this in a Codename One maven project, add the following dependency to your common/pom.xml dependencies section:

[source,xml]
----

com.codenameone
websockets-lib
1.0
pom

----

**For non-maven projects:**

Copy the cn1-websockets.cn1lib file https://github.com/shannah/cn1-websockets/releases/latest[from the latest release] into your project's `lib` directory. Then select "Refresh Libs".

== Chat Demo

image::https://cloud.githubusercontent.com/assets/2677562/9028518/beb5ecc6-3930-11e5-8db1-3d7635a37e37.png[Chat Demo]

You can try this out by running the chat demo app.

* https://github.com/shannah/cn1-websockets/tree/master/cn1-websockets-demo[Source] (Netbeans Project)
* http://translation.weblite.ca:8080/WebSocketDemo[Javascript Port]
* http://translation.weblite.ca:8080/cn1-demos/WebSocketDemo-release.apk[Android APK]
* http://translation.weblite.ca:8080/cn1-demos/CN1WebSockets.dmg[Mac OS X Desktop App]
* iOS (Build from https://github.com/shannah/cn1-websockets/tree/master/cn1-websockets-demo[source])

The companion JavaEE server project for this demo app can be found https://github.com/shannah/cn1-websockets/tree/master/cn1-websocket-demo-server[here].

== License

The MIT License (MIT)

Copyright (c) 2015 Steve Hannah

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

== Sample Usage

[source,java]
----
final WebSocket sock = new WebSocket("ws://10.0.1.21:8080/WebSocketServer/whiteboardendpoint") {

@Override
protected void onOpen() {
System.out.println("In onOpen");
}

@Override
protected void onClose(int statusCode, String reason) {
System.out.println("Connection closed");
}

@Override
protected void onMessage(String message) {
System.out.println("Received message "+message);
}

@Override
protected void onError(Exception ex) {
System.out.println("in onError");
}

@Override
protected void onMessage(byte[] message) {
// Received a binary message
}

}
.autoReconnect(5000); // Automatically reconnect if connection fails or server disconnects
// Attempts every 5 seconds until successful
sock.connect();

Form hi = new Form("Hi World");
hi.addComponent(new Button(new Command("Ping Peer") {

@Override
public void actionPerformed(ActionEvent evt) {
sock.send("Hello From Codename One");
}
}));
hi.show();
----

== Contributions

If you would like to build the source yourself, or make improvements, simply fork this project and build it locally.

=== Building From Source

----
$ git clone https://github.com/shannah/cn1-websockets.git
$ cd cn1-websockets-lib
$ mvn install
----

You should then be able to find the `cn1-websockets.cn1lib` in the common/target directory

==== Project Source Layout

The library project is contained in link:the cn1-websockets-lib[] directory. For details of project structure see https://shannah.github.io/codenameone-maven-manual/[The Codename One Maven Developers Guide].

== Contact

http://sjhannah.com/[Steve Hannah]