Skip to content
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

WebSocket API #17

Open
wants to merge 2 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
29 changes: 28 additions & 1 deletion src/main/kotlin/spark/kotlin/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,24 @@ fun internalServerError(function: RouteHandler.() -> Any) {
}
}

//----------------- TODO: Web sockets -----------------//
//----------------- Web sockets -----------------//

/**
* Registers a WebSocket handler class under a given URL.
* @param url The URL to attach the WebSocket handler to.
* @param handler A class annotated with Jetty's WebSocket annotations.
*/
fun webSocket(url: String, handler: KClass<*>) {
Spark.webSocket(url, handler.java)
}

/**
* Sets the idle timeout for WebSocket connections.
* @param timeout The amount of time, in milliseconds, that a connection can remain established for with no activity.
*/
fun webSocketIdleTimeoutMillis(timeout: Int) {
Spark.webSocketIdleTimeoutMillis(timeout)
}

//----------------- exception mapping -----------------//

Expand Down Expand Up @@ -473,6 +490,16 @@ class Http(val service: Service) {
//----------------- Redirect -----------------//
val redirect: Redirect = service.redirect

//----------------- WebSockets ------------------//
// These behave in exactly the same way as the static WebSocket methods.
fun webSocket(path: String, handler: KClass<*>) {
service.webSocket(path, handler.java)
}

fun webSocketIdleTimeoutMillis(timeout: Int) {
service.webSocketIdleTimeoutMillis(timeout)
}

/**
* Gets the port
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package spark.examples.instance

import org.omg.CosNaming.NamingContextPackage.NotFound
import spark.examples.testutil.SampleWebSocketHandler
import spark.kotlin.Http
import spark.kotlin.halt
import spark.kotlin.ignite
Expand All @@ -28,6 +28,8 @@ fun main(args: Array<String>) {

val http: Http = ignite()

http.webSocket("/ws", SampleWebSocketHandler::class)

http.staticFiles.location("/public")

http.get("/hello") {
Expand Down Expand Up @@ -75,4 +77,5 @@ fun main(args: Array<String>) {
}
}


class NotFoundException(message: String) : Exception(message)
2 changes: 2 additions & 0 deletions src/test/kotlin/spark/examples/static/StaticApiExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package spark.examples.static

import spark.examples.testutil.SampleWebSocketHandler
import spark.kotlin.*

/**
* Example usage of spark-kotlin via STATIC API.
*/
fun main(args: Array<String>) {
webSocket("/ws", SampleWebSocketHandler::class)

staticFiles.location("/public")

Expand Down
36 changes: 36 additions & 0 deletions src/test/kotlin/spark/examples/testutil/SampleWebSocketHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package spark.examples.testutil

import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.annotations.*
import java.io.InputStream

@WebSocket class SampleWebSocketHandler {
@OnWebSocketConnect
fun onConnect(s: Session) {
println("${s.remoteAddress} has opened a WebSocket connection.")
}

@OnWebSocketError
fun onError(s: Session, t: Throwable) {
println("An error was encountered on ${s.remoteAddress}' connection: $t")
}

// Handles text messages by echoing them back to the sender
@OnWebSocketMessage
fun onMessage(s: Session, msg: String) {
println("Message received from ${s.remoteAddress}: $msg")
s.remote.sendString(msg)
}

// Handles binary messages
@OnWebSocketMessage
fun onMessage(s: Session, input: InputStream) {
println("Binary message received from ${s.remoteAddress}")
}

@OnWebSocketClose
fun onClose(s: Session, code: Int, reason: String) {
println("${s.remoteAddress} has closed their connection: $code ($reason)")
}

}