Websockets library for Codename One
Java Objective-C C# JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cn1-websocket-demo-server
cn1-websockets-demo
cn1-websockets-lib
dist
.gitignore
README.adoc
build.xml
config.properties.sample

README.adoc

Codename One WebSockets Library

This library adds WebSocket client support for Codename One apps.

Supported Platforms

  1. JavaSE (Codename One Simulator)

  2. Android

  3. iOS

  4. Javascript

  5. Windows (UWP)

Installation

Copy the cn1-websockets.cn1lib file from the latest release into your project’s lib directory. Then select "Refresh Libs".

Building for iOS additionally requires the following build hints to be set:

  1. ios.add_libs=libicucore.dylib

  2. ios.objC = true

Chat Demo

Chat Demo

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

The companion JavaEE server project for this demo app can be found 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

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
     }

};
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
$ ant

You should then be able to find the cn1-websockets.cn1lib in the newly created dist directory.

Project Source Layout

Notice that the cn1-websockets-lib subproject is only used for building the project. Modify sources in the cn1-websockets-demo project. The build.xml script will automatically copy the necessary files into the cn1-websockets-lib project at build time. This just makes it easier for testing the library during development.

Contact