• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    Java
  • License
    Other
  • Created over 12 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vert.x Socket.IO Module

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 mod-socket-io.

Dependency

Add a maven repository contains mod-socket-io.

Gradle

repositories {
  maven { url 'https://github.com/keesun/mvn-repo/raw/master' }
  ...
}

Maven

<repository>
    <id>my.mvn.repo</id>
    <url>https://github.com/keesun/mvn-repo/raw/master</url>
</repository>

Add a dependency.

Gradle (for the Vert.x 2.0.0-final)

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

Gradle (for the Vert.x 2.0.0-CR3)

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

Gradle (for the Vert.x 1.3.1.final)

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

Maven (for the Vert.x 2.0.0-final)

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

Maven (for the Vert.x 2.0.0-CR3)

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

Maven (for the Vert.x 1.3.1.final)

<dependency>
    <groupId>com.nhncorp</groupId>
    <artifactId>mod-socket-io</artifactId>
    <version>0.9.0</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>

More Repositories

1

study

Learn, Share and Grow
Java
551
star
2

inflearn-the-java-test

더 ėžë°”, ė• í”ŒëĻŦėŧ€ė´ė…˜ė„ 테ėŠ¤íŠ¸í•˜ëŠ” 다ė–‘í•œ 방법
Java
76
star
3

spring-security-basic

Java
48
star
4

spring-vertx-sample

Sample project that using Spring MVC and Vertx embedded.
CSS
32
star
5

amugona

amugona
Java
29
star
6

javaservletdemo

ė¸í”„런, ėŠ¤í”„링 ė›š MVC 강ėĸŒ 1ëļ€ ėŊ”ë“œ
Java
15
star
7

demo-boot-web

ė¸í”„런, ėŠ¤í”„링 ė›š MVC 강ėĸŒ 2ëļ€ ėŊ”ë“œ
Java
12
star
8

adiue2014-moim

Building REST API Server with Spring Boot demo
Java
11
star
9

demo-web-mvc

ė¸í”„런 ėŠ¤í”„링 ė›š MVC 강ėĸŒ 3ëļ€ ėŊ”ë“œ
Java
11
star
10

demo-ksug-2019-test

Java
9
star
11

servlet2spring

Presentation Resources for the eGov Open Community
Java
8
star
12

swagger-noxml-mvc

Spring 3.1 based Web Application that has no XML.
Java
8
star
13

swagger-spring

swagger demo application
Java
7
star
14

OSAF

OpenSprout Application Framework
JavaScript
6
star
15

demo-ksug-2019-code-manipulation

Java
5
star
16

swagger-node

swagger demo application
JavaScript
5
star
17

easy_test

Test supporting library for the Spring and JPA based web application.
Java
5
star
18

legacy-sample

ė˜ˆė œ ėŊ”ë“œėž…니다.
Java
4
star
19

learning-programming

learning programming progject
Java
4
star
20

mod-sample

Vert.x module sample
Shell
3
star
21

start

sprign boot + spring data jpa + query dsl + spring security + bootstrap
Java
3
star
22

easy_jpa

Learning JPA project
Java
3
star
23

whiteboard

ė›ëž˜;; 게ė‹œíŒ 만들려던 프로ė íŠ¸ė¸ë° 발표ėšŠėœŧ로 ė¨ë˛„렸네;;
Java
3
star
24

moim-sample

Java
2
star
25

rest-api-sample

Java
2
star
26

changbal-java-test

Java
1
star
27

node-socket-demo-tester

node-socket-demo-tester
Java
1
star
28

node-socket-demo

node.js and socket.io example
JavaScript
1
star
29

spring-social-me2day

Spring Social for Me2day(that is a Korean SNS)
Java
1
star
30

demo-spring-boot-java

Spring Boot demo application
Java
1
star
31

his

Compare Hibernate, iBatis and Spring JDBC
Java
1
star
32

quiz4kids

Quiz A+
Swift
1
star
33

yes24-api-server

Groovy
1
star
34

keesun.github.io

HTML
1
star