This library adds WebSocket client support for Codename One apps.
For Maven Projects: To use this in a Codename One maven project, add the following dependency to your common/pom.xml dependencies section:
<dependency>
<groupId>com.codenameone</groupId>
<artifactId>websockets-lib</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
For non-maven projects:
Copy the cn1-websockets.cn1lib file from the latest release into your project’s lib
directory. Then select "Refresh Libs".
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.
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();
If you would like to build the source yourself, or make improvements, simply fork this project and build it locally.
$ 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
The library project is contained in link:the cn1-websockets-lib[] directory. For details of project structure see The Codename One Maven Developers Guide.