简体   繁体   中英

Connect to Scala Websocket from Android Emulator with React native

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?

  1. I tried connection to wss://echo.websocket.org which works pretty fine
  2. I tried with a basic html js page to connect to the websocket, which also works pretty good
  3. 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:)

Resolved the Problem:

I changed the Url (var wsUri) from localhost to my private ip adress ( I still don't know why this works:D)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM