我正在尝试在Scala中实现一个实时游戏服务器,但我在使用javascript的WebSocket连接它时遇到了问题。我尝试使用ws:// localhost:8888,ws://127.0.0.1:8888或ws:// [my ip]:8888与websocket.org/echo.html连接。
这是我的服务器代码,这几乎就是Akka医生的例子:
class MyServer(port: Int) extends Actor with ActorLogging{
var address: InetSocketAddress = new InetSocketAddress("", port)
var socketServer: ServerHandle = IOManager(context.system).listen(address)
override def receive = {
//Fired when the server is listening
case Listening(server, add) =>
log.info(s"The server is now listening on $add.")
//When a new client connect to the server...
case NewClient(server) =>
log.info("A new client just connected !")
server.accept()
//When a message is received by the server
case Read(socket, bytes) =>
log.info("The server just received some data.")
log.info(bytes.decodeString("US-ASCII"))
//When a connection with a client is closed
case Closed(socket, cause) =>
log.info(s"A client just disconnected : $cause")
}
}
object GemsApplication extends App{
println("Starting Gems application !")
val port = Option(System.getenv("PORT")).map(_.toInt).getOrElse(8888)
println(s"Selected port is $port.")
ActorSystem().actorOf(Props( new MyServer(port) ))
}
奇怪的是我可以使用Python套接字连接到我的服务器。由于它们可能更低级,我猜测我必须实现握手响应以使其与WebSocket一起使用,但我不应该看到来自Web套接字的消息要求升级?甚至只是连接?
感谢您提供的任何见解
罗宾