Giter VIP home page Giter VIP logo

justinmrousseau / websocket-android-phonegap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anismiles/websocket-android-phonegap

0.0 2.0 0.0 224 KB

This is a Java library that implements Websockt API (Draft-75/76) for Android platform. Library uses java.nio.* packages for efficient non-blocking evented behavior. It easily gets integrated with Phonegap framework.

Home Page: http://wp.me/pVyFz-qO

License: MIT License

HTML 1.13% JavaScript 4.10% Java 94.77%

websocket-android-phonegap's Introduction

Note: this plugin will be added to Phonegap-android core very soon. :) Here is the forked project: https://github.com/anismiles/phonegap-android


Websocket Android Plugin with Phonegap integration
--------------------------------------------------

This is a Java library that implements Websockt API (Draft-75/76) for Android platform. Library uses java.nio.* packages for
efficient non-blocking evented behavior. It easily gets integrated with Phonegap framework too.

Usage (native Android)
----------------------

1. Copy Java source into your source folder.
2. Create a class, say WebSocketListener extending WebSocket (refer: com.strumsoft.websocket.WebSocket)
3. Implement following methods
	1. onOpen
	2. onClose
	3. onMessage
	4. onReconnect (WebSocketListener will try to reconnect to the server in case of connection failure)
	 
Usage (Phonegap)
----------------

1. Copy Java source into your source folder.
2. Copy websocket.js in your assets/www/js folder
3. Attach com.strumsoft.websocket.phonegap.WebSocketFactory to WebView, like this:

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.loadUrl(" file:///android_asset/www/index.html ");

		// attach websocket factory
		appView.addJavascriptInterface(new WebSocketFactory(appView), "WebSocketFactory");
	}

4. In your page, create a new WebSocket, and overload its method 'onmessage', 'onopen', 'onclose', like this:

	// new socket
	var socket = new WebSocket('ws://192.168.1.153:8081');
	 
	// push a message after the connection is established.
	socket.onopen = function() {
	 socket.send('{ "type": "join", "game_id": "game/6"}')
	};

	// alerts message pushed from server
	socket.onmessage = function(msg) {
	 alert(JSON.stringify(msg.data));
	};

	// alert close event
	socket.onclose = function() {
	 alert('closed');
	};


ps: It doesn't support 'onerror' event, and various states as defined by WebSocket APIs yet. I am working on it. By the way, if you like the project, join the force. 

websocket-android-phonegap's People

Contributors

pdobrev avatar

Watchers

James Cloos avatar Justin M Rousseau, CEO avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.