Git Product home page Git Product logo

mod-socket-io's Introduction

Socket.io for Vert.x

This module allows the Vert.x users can make a socket.io server as node.js users do. Now, This module supports the latest version of the socket.io, 0.9.10.

Name

The module name is socket-io.

Dependency

Add a maven repository contains mod-socket-io.

Gradle

repositories {
  maven { url 'http://dev.springsprout.org/nexus/content/groups/public/' }
  ...
}

Maven

<repositories>
  <repository>
    <id>springsprout</id>
    <url>http://dev.springsprout.org/nexus/content/groups/public/</url>
  </repository>
</repositories>

Add a dependency.

Gradle

dependencies {
  compile      "com.nhncorp:mod-socket-io:0.9.10"
  ...
}

Maven

<dependency>
  <groupId>com.nhncorp</groupId>
  <artifactId>mod-socket-io</artifactId>
  <version>0.9.10</version>
</dependency>

Configuration

You can configure everything that you can configure in the Socket.io like:

	io.configure(new Configurer() {
		public void configure(JsonObject config) {
			config.putString("transports", "websocket,flashsocket,htmlfile,xhr-polling,jsonp-polling");
			config.putBoolean("authorization", true);
		}
	});

Examples

Verticle

You can use this module in a simple Verticle like:

	import com.nhncorp.mods.socket.io.SocketIOServer;
	import com.nhncorp.mods.socket.io.SocketIOSocket;
	import com.nhncorp.mods.socket.io.impl.DefaultSocketIOServer;
	import org.vertx.java.core.Handler;
	import org.vertx.java.core.http.HttpServer;
	import org.vertx.java.core.json.JsonObject;
	import org.vertx.java.deploy.Verticle;

	/**
	 * @author Keesun Baik
	 */
	public class SampleVerticle extends Verticle {

		@Override
		public void start() throws Exception {
			int port = 9090;
			HttpServer server = vertx.createHttpServer();
			SocketIOServer io = new DefaultSocketIOServer(vertx, server);

			io.sockets().onConnection(new Handler<SocketIOSocket>() {
				public void handle(final SocketIOSocket socket) {
					socket.on("timer", new Handler<JsonObject>() {
						public void handle(JsonObject event) {
							socket.emit("timer", event);
						}
					});
				}
			});

			System.out.println("server is running on http://localshot:" + port);
			server.listen(port);
		}
}

The code is located in samples/verticle/SampleVerticle.java.

You can run this module by vertx run but before run this simple verticle, you should put some jars to the classpath.

Here is some options you can use.

  • simply add all files in the dist directory to your VERTX_HOME/libs directory
  • or use -cp option when you run the verticle.

Now, you can run the verticle like:

samples/verticle> vertx run SampleVerticle.java

Module

First, you should include this module's resource by includes:.

{
	"main": "package.to.your.RunnableClassName",
	"includes": "com.nhncorp.socket-io-v0.9.10"
}

And, after you put the module's jar file to you module's classpath. You can code like:

	public class RunnableClassName extends Verticle {

		@Override
		public void start() {
			HttpServer server = vertx.createHttpServer();
			SocketIOServer io = new DefaultSocketIOServer(vertx, server);

			io.sockets().onConnection(new Handler<SocketIOSocket>() {
				public void handle(final SocketIOSocket socket) {
					JsonObject data = new JsonObject();
					data.putString("hello", "world");
					socket.emit("news", data);

					socket.on("my other event", new Handler<JsonObject>() {
						public void handle(JsonObject data) {
							System.out.println(data);
						}
					});
				}
			});

			server.listen(9090);
		}
	}

In the view, you can use the same socket.io javascript like:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script>
	var socket = io.connect('http://localhost:9090');
	socket.on('news', function (data) {
		console.log(data);
		socket.emit('my other event', { my: 'data' });
	});
</script>

mod-socket-io's People

Contributors

whiteship avatar

Watchers

 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.