GithubHelp home page GithubHelp logo

meimingle / tinyweb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tejmagar/tinyweb

0.0 0.0 0.0 122 KB

TinyWeb is a simple Android library based on NanoHTTPD. This library handles routing and middlewares features.

Java 100.00%

tinyweb's Introduction

TinyWeb

TinyWeb is a simple Android library based on NanoHTTPD. This library handles routing and middlewares features.

Add jitpack in repositories

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Install gradle dependencies

Without WebSocket

dependencies {
    implementation 'com.github.tejmagar:tinyweb:1.0.0'
    implementation 'org.nanohttpd:nanohttpd:2.3.1'
}

With WebSocket

dependencies {
    implementation 'com.github.tejmagar:tinyweb:1.0.0'
    implementation 'org.nanohttpd:nanohttpd-websocket:2.3.1'
}

Creating a View

import org.tinyweb.views.View;
import fi.iki.elonen.NanoHTTPD;

public class HomeView extends View {

    @Override
    public NanoHTTPD.Response getResponse(NanoHTTPD.IHTTPSession request) {
        return NanoHTTPD.newFixedLengthResponse("Hello World");
    }
}

Creating a server

TinyWeb tinyWeb = new TinyWeb(8000);
Routes routes = new Routes();
routes.addRoute(new Path("/", new HomeView()));
tinyWeb.setRoutes(routes);

try {
    tinyWeb.start();
} catch (IOException e) {
    throw new RuntimeException(e);
}

Creating a server with WebSocket

Creating a WsView

import org.tinyweb.views.WsView;
import org.tinyweb.websocket.TinyWebSocket;

import java.io.IOException;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoWSD;

public class MyWsView extends WsView {

    @Override
    public NanoWSD.WebSocket webSocket(NanoHTTPD.IHTTPSession request) {
        return new MyTinyWebSocket(request);
    }

    static class MyTinyWebSocket extends TinyWebSocket {

        public MyTinyWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
            super(handshakeRequest);
        }

        @Override
        protected void onOpen() {
            super.onOpen();
            try {
                send("Connected");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void onMessage(NanoWSD.WebSocketFrame messageFrame) {
            super.onMessage(messageFrame);
            System.out.println(messageFrame.getTextPayload());
        }
    }
}

Use TinyWebWs class for Web Server

Routes routes = new Routes();
routes.addRoute(new Path("/", new HomeView()));

Routes wsRoutes = new Routes();
wsRoutes.addRoute(new Path("/ws/", new MyWsView()));

TinyWebWS tinyWeb = new TinyWebWS(8000);
tinyWeb.setRoutes(routes);
tinyWeb.setWsRoutes(wsRoutes);

try {
    tinyWeb.start();
} catch (IOException e) {
    throw new RuntimeException(e);
}

Tips

Running server in foreground service helps to serve pages in background even after closing app.

tinyweb's People

Contributors

tejmagar 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.