-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1ae6aa
commit acb9b25
Showing
6 changed files
with
642 additions
and
127 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
Good example | ||
chat | ||
|
||
https://github.com/dnvriend/akka-http-test | ||
http://www.diversit.eu/2015/07/using-akka-http-to-perform-rest-call.html | ||
http://alexkuang.com/blog/2016/04/26/writing-an-api-client-with-akka-http/ | ||
https://markatta.com/codemonkey/blog/2016/04/18/chat-with-akka-http-websockets/ | ||
https://markatta.com/codemonkey/blog/2016/10/02/chat-with-akka-http-websockets/ | ||
|
||
POST | ||
http://stackoverflow.com/questions/32757605/idiomatic-way-to-create-a-basic-http-post-request-with-akka-http | ||
|
||
TESTING WEB SOCKET STUFF | ||
|
||
1. Run server | ||
2. Browse to ChatTestClient.Html in browser | ||
3. Open Postman to http://localhost:8080/dowork this should send some responses to the websocket client opened in browser |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,8 @@ | ||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.marshalling.Marshal | ||
import akka.http.scaladsl.model._ | ||
import akka.http.scaladsl.unmarshalling.Unmarshal | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.scaladsl._ | ||
import scala.concurrent.{Await, Future} | ||
import concurrent.ExecutionContext.Implicits.global | ||
import common.{Item, JsonSupport} | ||
import concurrent.duration._ | ||
import scala.io.StdIn | ||
|
||
object Demo extends App with JsonSupport { | ||
|
||
implicit val system = ActorSystem() | ||
implicit val materializer = ActorMaterializer() | ||
|
||
val httpClient = Http().outgoingConnection(host = "localhost", port = 8080) | ||
|
||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
// GET http://localhost:8080/randomitem | ||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
val randomItemUrl = s"""/randomitem""" | ||
private val flowGet : Future[Item] = | ||
Source.single(HttpRequest(method = HttpMethods.GET,uri = Uri(randomItemUrl))) | ||
.via(httpClient) | ||
.mapAsync(1)(response => Unmarshal(response.entity).to[Item]) | ||
.runWith(Sink.head) | ||
val start = System.currentTimeMillis() | ||
val result = Await.result(flowGet, 5 seconds) | ||
val end = System.currentTimeMillis() | ||
println(s"Result in ${end-start} millis: $result") | ||
|
||
|
||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
// POST http://localhost:8080/saveitem | ||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
val saveItemUrl = s"""/saveitem""" | ||
val itemToSave = Item("newItemHere",12) | ||
val flowPost = for { | ||
requestEntity <- Marshal(itemToSave).to[RequestEntity] | ||
response <- | ||
Source.single(HttpRequest(method = HttpMethods.POST,uri = Uri(saveItemUrl), entity = requestEntity)) | ||
.via(httpClient) | ||
.mapAsync(1)(response => Unmarshal(response.entity).to[Item]) | ||
.runWith(Sink.head) | ||
} yield response | ||
val startPost = System.currentTimeMillis() | ||
val resultPost = Await.result(flowPost, 5 seconds) | ||
val endPost = System.currentTimeMillis() | ||
println(s"Result in ${endPost-startPost} millis: $resultPost") | ||
|
||
object Demo extends App { | ||
|
||
val regularRoutesDemo = new RegularRoutesDemo() | ||
regularRoutesDemo.Run() | ||
StdIn.readLine() | ||
} |
64 changes: 64 additions & 0 deletions
64
AkkaHttp/clientside/src/main/scala-2.11/RegularRoutesDemo.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.marshalling.Marshal | ||
import akka.http.scaladsl.model._ | ||
import akka.http.scaladsl.unmarshalling.Unmarshal | ||
import akka.stream.ActorMaterializer | ||
import akka.stream.scaladsl._ | ||
import scala.concurrent.{Await, Future} | ||
import concurrent.ExecutionContext.Implicits.global | ||
import common.{Item, JsonSupport} | ||
import concurrent.duration._ | ||
import scala.io.StdIn | ||
|
||
class RegularRoutesDemo extends JsonSupport { | ||
|
||
def Run() : Unit = { | ||
implicit val system = ActorSystem() | ||
implicit val materializer = ActorMaterializer() | ||
|
||
val httpClient = Http().outgoingConnection(host = "localhost", port = 8080) | ||
|
||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
// GET http://localhost:8080/randomitem | ||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
val randomItemUrl = s"""/randomitem""" | ||
val flowGet : Future[Item] = | ||
Source.single( | ||
HttpRequest( | ||
method = HttpMethods.GET, | ||
uri = Uri(randomItemUrl)) | ||
) | ||
.via(httpClient) | ||
.mapAsync(1)(response => Unmarshal(response.entity).to[Item]) | ||
.runWith(Sink.head) | ||
val start = System.currentTimeMillis() | ||
val result = Await.result(flowGet, 5 seconds) | ||
val end = System.currentTimeMillis() | ||
println(s"Result in ${end-start} millis: $result") | ||
|
||
|
||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
// POST http://localhost:8080/saveitem | ||
//+++++++++++++++++++++++++++++++++++++++++++++++ | ||
val saveItemUrl = s"""/saveitem""" | ||
val itemToSave = Item("newItemHere",12) | ||
val flowPost = for { | ||
requestEntity <- Marshal(itemToSave).to[RequestEntity] | ||
response <- | ||
Source.single( | ||
HttpRequest( | ||
method = HttpMethods.POST, | ||
uri = Uri(saveItemUrl), | ||
entity = requestEntity) | ||
) | ||
.via(httpClient) | ||
.mapAsync(1)(response => Unmarshal(response.entity).to[Item]) | ||
.runWith(Sink.head) | ||
} yield response | ||
val startPost = System.currentTimeMillis() | ||
val resultPost = Await.result(flowPost, 5 seconds) | ||
val endPost = System.currentTimeMillis() | ||
println(s"Result in ${endPost-startPost} millis: $resultPost") | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
AkkaHttp/serverside/src/main/scala-2.11/WebSocketTestClient.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<html> | ||
<head> | ||
<script src="https://code.jquery.com/jquery-2.2.3.min.js" | ||
integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" | ||
crossorigin="anonymous"></script> | ||
<title>Chat</title> | ||
</head> | ||
<body> | ||
<textarea id="message"></textarea> | ||
<button id="send">Send</button> | ||
<ul id="messages"></ul> | ||
|
||
|
||
<script language="javascript"> | ||
var $messages = $("#messages"), | ||
$send = $("#send"), | ||
$message = $("#message"), | ||
connection = new WebSocket('ws://localhost:8080/websocket') | ||
$send.prop("disabled",true) | ||
connection.onopen = function () { | ||
$send.prop("disabled",false) | ||
$messages.prepend($("<li>Connected</li>")) | ||
$send.on('click', function() { | ||
var text = $message.val() | ||
$message.val("") | ||
connection.send(text) | ||
}) | ||
} | ||
connection.onerror = function (error) { console.log('WebSocket Error ', error) } | ||
connection.onmessage = function (event) { | ||
$messages.prepend($("<li>" + event.data + "</li>")) | ||
} | ||
</script> | ||
</body> | ||
</html> | ||
Contact GitHub |