Skip to content

Abstract transports & and add akka-http http/websocket backend #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Java backend for `Socket.IO` library (http://socket.io/)
Java/Scala backend for `Socket.IO` library (http://socket.io/)

Supports `Socket.IO` clients version 1.0+
Requires JSR 356-compatible server (tested with Jetty 9 and Tomcat 8)

There is currently support for JSR 356-compatible servlet containers (tested with Jetty 9 and Tomcat 8) and Akka-HTTP.

Right now only websocket and XHR polling transports are implemented.

Expand Down Expand Up @@ -41,3 +42,21 @@ When Jetty server is embedded into your application, but websocket endpoint is e
});
```
See example in [com.codeminders.socketio.sample.jetty.ChatServer](https://github.com/codeminders/socket.io-server-java/blob/master/samples/jetty/src/main/java/com/codeminders/socketio/sample/jetty/ChatServer.java)

## Akka-HTTP usage

The akka-http support revolves around a single exposed class, `SocketIOAkkaHttp`.

```scala
val socketIO = SocketIOAkkaHttp().fold(sys.error, identity)

val routes: Route = {
pathPrefix("socket.io") {
socketIO.route
}
}

Http().bindAndHandle(routes, "localhost", 8080)
```

The socket URL can be changed by moving the `.route` call to another area in your routes hierarchy. To configure limits and timeouts, an optional `SocketIOAkkaHttpSettings` instance can be passed to the `SocketIOAkkaHttp()` constructor.
39 changes: 29 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-parent</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Socket.IO Java Server</name>
Expand All @@ -15,7 +15,7 @@
<inceptionYear>2010</inceptionYear>

<properties>
<jdk.version>1.7</jdk.version>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -69,6 +69,14 @@
<role>contributor</role>
</roles>
</contributor>
<contributor>
<name>Brian Tarricone</name>
<email>[email protected]</email>
<timezone>-8</timezone>
<roles>
<role>contributor</role>
</roles>
</contributor>
</contributors>

<licenses>
Expand All @@ -92,7 +100,9 @@

<modules>
<module>socket-io</module>
<module>socket-io-servlet</module>
<module>samples</module>
<module>socket-io-akka-http-scala</module>
</modules>

<distributionManagement>
Expand Down Expand Up @@ -140,8 +150,6 @@
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
Expand Down Expand Up @@ -277,13 +285,24 @@
</plugins>
</reporting>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
14 changes: 11 additions & 3 deletions samples/chat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-sample</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>socketio-sample-chat</artifactId>
Expand All @@ -19,8 +19,16 @@
<dependencies>
<dependency>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socket-io</artifactId>
<version>1.0.9</version>
<artifactId>socket-io-servlet</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.codeminders.socketio.common.DisconnectReason;
import com.codeminders.socketio.common.SocketIOException;
import com.codeminders.socketio.server.*;
import com.codeminders.socketio.server.transport.websocket.WebsocketIOServlet;
import com.codeminders.socketio.server.servlet.transport.websocket.WebsocketIOServlet;
import com.google.common.io.ByteStreams;

import javax.servlet.ServletConfig;
Expand Down
6 changes: 2 additions & 4 deletions samples/jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-sample</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>socketio-sample-chat-jetty</artifactId>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-sample-chat</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>
<dependency>
Expand All @@ -43,12 +43,10 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package com.codeminders.socketio.sample.jetty;

import com.codeminders.socketio.sample.chat.ChatSocketServlet;
import com.codeminders.socketio.server.transport.websocket.WebsocketTransportConnection;
import com.codeminders.socketio.server.servlet.transport.websocket.WebsocketTransportConnection;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
Expand Down
2 changes: 1 addition & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-parent</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>socketio-sample</artifactId>
Expand Down
6 changes: 2 additions & 4 deletions samples/tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-sample</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>socketio-sample-chat-tomcat</artifactId>
Expand All @@ -23,17 +23,15 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socketio-sample-chat</artifactId>
<version>1.0.9</version>
<version>2.0.0-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>
<dependency>
Expand Down
157 changes: 157 additions & 0 deletions socket-io-akka-http-scala/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>socketio-parent</artifactId>
<groupId>com.codeminders.socketio</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>socket-io-akka-http-scala_${scala.binary.version}</artifactId>
<packaging>jar</packaging>

<properties>
<scala.binary.version>2.13</scala.binary.version>
<scala.version>2.13.2</scala.version>
<akka.version>2.6.5</akka.version>
<akka-http.version>10.1.12</akka-http.version>
</properties>

<profiles>
<profile>
<id>scala-2.13</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<scala.binary.version>2.13</scala.binary.version>
<scala.version>2.13.2</scala.version>
</properties>
</profile>
<profile>
<id>scala-2.12</id>
<properties>
<scala.binary.version>2.12</scala.binary.version>
<scala.version>2.12.10</scala.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<configuration>
<recompileMode>incremental</recompileMode>
<args>
<arg>-Ypartial-unification</arg>
<arg>-Xlint:unsound-match</arg>
<arg>-Yno-adapted-args</arg>
<arg>-Ywarn-infer-any</arg>
<arg>-Ywarn-nullary-unit</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>com.codeminders.socketio</groupId>
<artifactId>socket-io</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>

<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_${scala.binary.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_${scala.binary.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-core_${scala.binary.version}</artifactId>
<version>${akka-http.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_${scala.binary.version}</artifactId>
<version>${akka-http.version}</version>
</dependency>
</dependencies>

<build>
<directory>target-${scala.binary.version}</directory>
<outputDirectory>target-${scala.binary.version}/classes</outputDirectory>
<testOutputDirectory>target-${scala.binary.version}/test-classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.3.1</version>
<configuration>
<recompileMode>incremental</recompileMode>
<args>
<arg>-deprecation</arg>
<arg>-encoding</arg><arg>utf-8</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-Xlint:infer-any</arg>
<arg>-Xlint:missing-interpolator</arg>
<arg>-Xlint:nullary-unit</arg>
<arg>-Xlint:private-shadow</arg>
<arg>-Xlint:type-parameter-shadow</arg>
<arg>-Ywarn-dead-code</arg>
<arg>-Ywarn-extra-implicit</arg>
<arg>-Ywarn-unused:imports</arg>
<arg>-Ywarn-unused:locals</arg>
<arg>-Ywarn-unused:privates</arg>
</args>
<charset>UTF-8</charset>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.spurint.maven.plugins</groupId>
<artifactId>scala-cross-maven-plugin</artifactId>
<version>0.2.1</version>
<executions>
<execution>
<id>rewrite-pom</id>
<goals>
<goal>rewrite-pom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions socket-io-akka-http-scala/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
com.codeminders.socket-io.server.akka-http {
# Whether or not to allow all origins via CORS
allow-all-origins = true
# A list of CORS allowed origins (only used if 'allow-all-origins' is false)
allowed-origins = []
# Interval at which to expect application-level pings
ping-interval = 25 seconds
# Time after which a connection will be considered dead if a ping has not been received
ping-timeout = 60 seconds
}
Loading