GithubHelp home page GithubHelp logo

sirithink / webbit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webbit/webbit

0.0 2.0 0.0 14.75 MB

A Java event based WebSocket and HTTP server

Home Page: http://webbitserver.org/

License: Other

webbit's Introduction

Webbit - A Java event based WebSocket and HTTP server

Build Status

Getting it

Prebuilt JARs are available from the central Maven repository or the Sonatype Maven repository.

Alternatively, you can get the latest code from Git and build it yourself:

git clone git://github.com/webbit/webbit.git
cd webbit

Make

Build is done with make. On OS-X and Linux this should work out of the box. On Solaris, use gmake. On Windows you will need Cygwin.

make

Maven

mvn install

Quick start

Start a web server on port 8080 and serve some static files:

WebServer webServer = WebServers.createWebServer(8080)
            .add(new StaticFileHandler("/web")) // path to web content
            .start()
            .get();

That was easy.

Now let's build a WebSocketHandler.

public class HelloWebSockets extends BaseWebSocketHandler {
    private int connectionCount;

    public void onOpen(WebSocketConnection connection) {
        connection.send("Hello! There are " + connectionCount + " other connections active");
        connectionCount++;
    }

    public void onClose(WebSocketConnection connection) {
        connectionCount--;
    }

    public void onMessage(WebSocketConnection connection, String message) {
        connection.send(message.toUpperCase()); // echo back message in upper case
    }

    public static void main(String[] args) {
        WebServer webServer = WebServers.createWebServer(8080)
                .add("/hellowebsocket", new HelloWebSockets())
                .add(new StaticFileHandler("/web"));
        webServer.start();
        System.out.println("Server running at " + webServer.getUri());
    }
}

And a page that uses the WebSocket (web/index.html)

<html>
  <body>

    <!-- Send text to websocket -->
    <input id="userInput" type="text">
    <button onclick="ws.send(document.getElementById('userInput').value)">Send</button>

    <!-- Results -->
    <div id="message"></div>

    <script>
      function showMessage(text) {
        document.getElementById('message').innerHTML = text;
      }

      var ws = new WebSocket('ws://' + document.location.host + '/hellowebsocket');
      showMessage('Connecting...');
      ws.onopen = function() { showMessage('Connected!'); };
      ws.onclose = function() { showMessage('Lost connection'); };
      ws.onmessage = function(msg) { showMessage(msg.data); };
    </script>
  </body>
</html>

Contributing

Running JUnit tests

mvn clean test

or

make clean test

Running Autobahn tests

Autobahn is a WebSocket server implemented in Python that comes with an extensive test suite that can be used to test other WebSocket servers as well.

We're using it to test Webbit.

Installing Autobahn

git submodule update --init

Running Autobahn tests

In shell A:

make echo

In shell B:

make autobahn

Open reports/servers/index.html to see the results.

More

webbit's People

Contributors

aslakhellesoy avatar bonifaido avatar chtheis avatar ebaxt avatar illicitonion avatar jabley avatar joewalnes avatar kushalp avatar manuel-woelker avatar nmische avatar normanmaurer avatar npryce avatar osi avatar petergillardmoss avatar ph2734 avatar rykov avatar s1monw avatar

Watchers

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