-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Showing
12 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -17,5 +17,5 @@ trait Routes{ | |
metadata0 = routes | ||
} | ||
|
||
def log: cask.util.Logger | ||
implicit def log: cask.util.Logger | ||
} |
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
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
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
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
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
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
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,21 @@ | ||
package app | ||
|
||
case class Websockets3()(implicit val log: cask.Logger) extends cask.Routes{ | ||
@cask.websocket("/connect/:userName") | ||
def showUserProfile(userName: String): cask.WebsocketResult = { | ||
if (userName != "haoyi") cask.Response("", statusCode = 403) | ||
else cask.WsHandler { channel => | ||
cask.WsActor { | ||
case cask.WsActor.Text("") => channel.send(cask.WsActor.Close()) | ||
case cask.WsActor.Text(data) => | ||
channel.send(cask.WsActor.Text(userName + " " + data)) | ||
} | ||
} | ||
} | ||
|
||
initialize() | ||
} | ||
|
||
object Websockets3Main extends cask.Main{ | ||
val allRoutes = Seq(Websockets3()) | ||
} |
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,75 @@ | ||
package app | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
import org.asynchttpclient.ws.{WebSocket, WebSocketListener, WebSocketUpgradeHandler} | ||
import utest._ | ||
|
||
object ExampleTests extends TestSuite{ | ||
|
||
|
||
def withServer[T](example: cask.main.Main)(f: String => T): T = { | ||
val server = io.undertow.Undertow.builder | ||
.addHttpListener(8080, "localhost") | ||
.setHandler(example.defaultHandler) | ||
.build | ||
server.start() | ||
val res = | ||
try f("http://localhost:8080") | ||
finally server.stop() | ||
res | ||
} | ||
|
||
val tests = Tests{ | ||
test("Websockets") - withServer(Websockets3Main){ host => | ||
@volatile var out = List.empty[String] | ||
val client = org.asynchttpclient.Dsl.asyncHttpClient(); | ||
try{ | ||
|
||
// 4. open websocket | ||
val ws: WebSocket = client.prepareGet("ws://localhost:8080/connect/haoyi") | ||
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener( | ||
new WebSocketListener() { | ||
|
||
override def onTextFrame(payload: String, finalFragment: Boolean, rsv: Int) { | ||
out = payload :: out | ||
} | ||
|
||
def onOpen(websocket: WebSocket) = () | ||
|
||
def onClose(websocket: WebSocket, code: Int, reason: String) = () | ||
|
||
def onError(t: Throwable) = () | ||
}).build() | ||
).get() | ||
|
||
// 5. send messages | ||
ws.sendTextFrame("hello") | ||
ws.sendTextFrame("world") | ||
ws.sendTextFrame("") | ||
Thread.sleep(100) | ||
out ==> List("haoyi world", "haoyi hello") | ||
|
||
var error: String = "" | ||
val cli2 = client.prepareGet("ws://localhost:8080/connect/nobody") | ||
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener( | ||
new WebSocketListener() { | ||
|
||
def onOpen(websocket: WebSocket) = () | ||
|
||
def onClose(websocket: WebSocket, code: Int, reason: String) = () | ||
|
||
def onError(t: Throwable) = { | ||
error = t.toString | ||
} | ||
}).build() | ||
).get() | ||
|
||
assert(error.contains("403")) | ||
|
||
} finally{ | ||
client.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import mill._, scalalib._ | ||
|
||
|
||
trait AppModule extends ScalaModule{ | ||
def scalaVersion = "2.13.0" | ||
def ivyDeps = Agg[Dep]( | ||
) | ||
object test extends Tests{ | ||
def testFrameworks = Seq("utest.runner.Framework") | ||
|
||
def ivyDeps = Agg( | ||
ivy"com.lihaoyi::utest::0.7.1", | ||
ivy"com.lihaoyi::requests::0.2.0", | ||
ivy"org.asynchttpclient:async-http-client:2.5.2" | ||
) | ||
} | ||
} |
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