Created
August 15, 2017 02:16
-
-
Save buster84/11f0e86bef7e1306394523eb81c2671e to your computer and use it in GitHub Desktop.
Not sending back WebSocket Connection Close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ws = require("nodejs-websocket") | |
var server = ws.createServer(function (conn) { | |
console.log("New connection") | |
var task = setInterval(function() { | |
conn.send("tick"); | |
}, 1000); | |
conn.on("close", function (code, reason) { | |
console.log("Connection closed"); | |
clearInterval(task) | |
}) | |
}).listen(9000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.{ ActorRef, ActorSystem } | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.Http.ServerBinding | |
import akka.http.scaladsl.server.Route | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.model.ws.{ Message, TextMessage, WebSocketRequest } | |
import akka.stream.ActorMaterializer | |
import akka.stream.scaladsl.{ Flow, Sink, Source, Keep } | |
import akka.util.Timeout | |
import scala.concurrent.{ ExecutionContext, Future } | |
import scala.concurrent.duration._ | |
import scala.io.StdIn | |
//#main-class | |
object InfinitOutputServer extends App { | |
// set up ActorSystem and other dependencies here | |
implicit val system: ActorSystem = ActorSystem("helloAkkaHttpServer") | |
implicit val materializer: ActorMaterializer = ActorMaterializer() | |
// Needed for the Future and its methods flatMap/onComplete in the end | |
implicit val executionContext: ExecutionContext = system.dispatcher | |
val route = pathSingleSlash { | |
get { | |
println("Got conn") | |
handleWebSocketMessages(Flow.fromSinkAndSource( | |
Sink.foreach(println), | |
Source.tick(1.second, 1.second, "tick").alsoTo(Sink.foreach(println)).map(TextMessage(_)) | |
// Source(List.fill(1000)("tick")).alsoTo(Sink.foreach(println)).map(TextMessage(_)) | |
)) | |
} | |
} | |
val serverBindingFuture: Future[ServerBinding] = Http().bindAndHandle(route, "localhost", 9000) | |
println(s"Server online at http://localhost:9000/\nPress RETURN to stop...") | |
StdIn.readLine() | |
serverBindingFuture | |
.flatMap(_.unbind()) | |
.onComplete { done => | |
done.failed.map { ex => println("Failed binding") } | |
system.terminate() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment