I'm recently developing a Chat Application with React Native v40 and Expo v40. Everything works fine but I can not connect to my Scala(2.13.3) Websocket. The Socket is built with Play (2.8)
To make it more clear:
Client.ts
var wsUri = "ws://localhost:9000/ws";
let websocket: WebSocket
export function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt: Event) { onOpen(evt) };
websocket.onclose = function (evt: Event) { onClose(evt) };
websocket.onmessage = function (evt: MessageEvent<string>) { onMessage(evt) };
websocket.onerror = function (evt: Event) { onError(evt) };
}
function onOpen(evt: Event) {
doSend("Hello world");
}
function onClose(evt: Event) {
console.log("closing connection")
}
function onMessage(evt: MessageEvent<string>) {
console.log(evt.data)
}
function onError(evt: Event) {
console.error(evt)
}
export function doSend(message: string) {
websocket.send(message);
}
My Scala Websocket looks like this:
Controller.scala:
def ws: WebSocket = WebSocket.accept[String, String] { request =>
ActorFlow.actorRef { out =>
ChatMessagingActor.props(out)
}
}
My Actor:
object ChatMessagingActor {
def props(clientActorRef: ActorRef) = Props(new ChatMessagingActor(clientActorRef))
}
class ChatMessagingActor(clientActorRef: ActorRef) extends Actor {
val logger: Logger = play.api.Logger(getClass)
override def receive: Receive = {
case msg: String =>
println(msg)
clientActorRef ! msg
case None =>
clientActorRef ! s"Closing the connection as requested"
self ! PoisonPill
}
override def postStop(): Unit = println("Closing Messaging Socket")
}
So Whats the Problem now?
When I start the emulator (or even my physical device) I receive the following error
Object { isTrusted: false, message: "Failed to connect to localhost/127.0.0.1:9000" }
and the connection closes immediately
When I try to connect via Browser I dont get any Errors.
What Have I tried so far?
- I tried connection to
wss://echo.websocket.org
which works
pretty fine
- I tried with a basic html js page to connect to the websocket, which
also works pretty good
- I changed the Socket adress in client.ts to ws://127.0.0.1:9000/ws
I Think this might be a security Issue but I'm not quite sure. Do I miss some policy changes in Scala or is this an Android thing?
Any help will be appreciated. thanks for your time :)
question from:
https://stackoverflow.com/questions/65933716/connect-to-scala-websocket-from-android-emulator-with-react-native 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…