Created
February 23, 2018 09:15
-
-
Save georgeOsdDev/4a437aeb79c34d4b09fbb984cd662931 to your computer and use it in GitHub Desktop.
Xitrum websocket connection issue
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
package quickstart | |
import xitrum.Server | |
object Boot { | |
def main(args: Array[String]) { | |
Server.start() | |
Server.stopAtShutdown() | |
} | |
} | |
import xitrum.annotation.WEBSOCKET | |
import xitrum.{WebSocketAction, WebSocketText} | |
@WEBSOCKET("echo") | |
class EchoWebSocketActor extends WebSocketAction { | |
def execute() { | |
log.debug("onOpen") | |
context.become { | |
case WebSocketText(text) => | |
log.info("onTextMessage: " + text) | |
respondWebSocketText(text.toUpperCase) | |
} | |
} | |
override def postStop() { | |
log.debug("onClose") | |
super.postStop() | |
} | |
} |
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
name := "xitrum-ws-client-test" | |
version := "1.0-SNAPSHOT" | |
scalaVersion := "2.10.3" | |
resolvers += "SonatypeReleases" at "http://oss.sonatype.org/content/repositories/releases/" | |
libraryDependencies += "org.asynchttpclient" % "async-http-client" % "2.4.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
import org.asynchttpclient.DefaultAsyncHttpClientConfig | |
import org.asynchttpclient.DefaultAsyncHttpClient | |
import org.asynchttpclient.ws.WebSocketUpgradeHandler | |
import org.asynchttpclient.ws.WebSocketListener | |
import org.asynchttpclient.ws.WebSocket | |
object TestMain { | |
val URL = "ws://localhost:8000/echo" | |
private val client = { | |
val TIMEOUT_MS = 5 * 1000 | |
val conf = new DefaultAsyncHttpClientConfig.Builder() | |
.setConnectTimeout(TIMEOUT_MS) | |
.setRequestTimeout(TIMEOUT_MS) | |
.setReadTimeout(TIMEOUT_MS) | |
.setConnectTimeout(TIMEOUT_MS) | |
.setWebSocketMaxFrameSize(10 * 1024 * 1024) | |
.build() | |
new DefaultAsyncHttpClient(conf) | |
} | |
def main(args: Array[String]) { | |
for (i <- 0 until 1000) { | |
print(s"${i},") | |
test() | |
} | |
} | |
def test() { | |
val l = new MyWebSocketListener | |
val h = new WebSocketUpgradeHandler.Builder().addWebSocketListener(l).build() | |
val ws = client.prepareGet(URL).execute(h).get() | |
ws.sendCloseFrame().get() | |
} | |
class MyWebSocketListener extends WebSocketListener { | |
override def onError(t: Throwable): Unit = {} | |
override def onClose(websocket: WebSocket, code: Int, reason: String): Unit = {} | |
override def onOpen(websocket: WebSocket): Unit = {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment