Giter Club home page Giter Club logo

localnetwork's Introduction

LocalNetwork

Local network library for connecting Android devices on WiFi. One device serves as a server, others are clients. Library takes care of discovery of server by clients. Library uses services and separate threads with sockets to handle communication.

This library is nowhere near done. But you can get it from jcenter if you want:

compile 'com.github.jokr:localnet:0.1'

Usage

There are two main parts of this library: server and client. One device should be server and others should be clients. Server uses LocalServer class, and clients LocalClient class.

There are two phases: discovery and session. In discovery phase client devices discover server on wifi.

Discovery | Client

Client should start LocalClient instance and call connect(). You will probably want to setDiscoveryReceiver(*DiscoveryStatusReceiver impl*) then you can get notified if server is discovered successfully. You will be also notified when server starts session.

public interface DiscoveryStatusReceiver {
        public void onDiscoveryTimeout();
        public void onServerDiscovered();
        public void onSessionStart();
    }

Discovery | Server

Server should start LocalServer instance and call init(). From there it is listening for clients to discover it.

localServer = new LocalServer(getContext());
localServer.init();

You can receive information when client connects by providing receiver in localServer.setReceiver method.

    @Override
    public void onClientConnected(Payload<?> payload) {
        // do something
    }

You will see a lot of Payload<?> through this library. It is basically a way for library to send any your object. Make your own class implements Serializable and pack it with Payload: new Payload<YourClass>(new YourClass(...)).

Once you are ready to end discovery and start session call localServer.setSession(*LocalSesion impl) and discovery phase ends and session starts.

LocalSession class

Main part of session is LocalSession class. This is abstract class you should extend and implement all session/game logic here. This code will run in service. Main methods to override are:

    @Override
    public void onCreate(Bundle bundle, ConnectedClients connectedClients){
        // session is created
    }

    @Override
    public void onReceiveMessage(long recipientId, Payload payload) {
        // received message from client
    }

    @Override
    public void onEvent(Payload<?> payload) {
        // received event from LocalServer instance
    }

From here you can call sendMessage(long recipientId, Payload<?> payload) to send message to client, or sendBroadcastMessage(Payload<?> payload) to send message to all clients. You can also send message to your LocalServer instance and then possibly show it on UI with sendUiEvent(Payload<?> payload)

Session | Server

To start a session, just call localServer.setSession(*LocalSesion impl*) and provide implementation as descibed above. Just like you can send events from your LocalSession to LocalServer, you can send events other way - from LocalServer to your LocalSession implementation by calling localServer.sendLocalSessionEvent(Payload<?>) and this will call onEvent method in your LocalSession.

Session | Client

Client can send messages to server by calling localClient.sendSessionMessage(Payload<?>) and receive messages from server by setting receiver:

localClient.setReceiver(new LocalClient.MessageReceiver() {
            @Override
            public void onMessageReceived(Payload<?> payload) {
                // Payload has object you've sent from LocalSession
            }
        });

localnetwork's People

Contributors

itsjokr avatar

Watchers

James Cloos avatar Nikhil 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.