Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

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 :)

question from:https://stackoverflow.com/questions/65933716/connect-to-scala-websocket-from-android-emulator-with-react-native

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...