Android Question Websocket with punycode urls?

CGP

Member
Licensed User
Longtime User
When I try to connect to an url like ws://mañana.com I get an error. Here is the log:

B4X:
Error occurred on line: 19 (WebSocketHandler)
de.tavendo.autobahn.WebSocketException: no host specified in WebSockets URI
    at de.tavendo.autobahn.WebSocketConnection.connect(WebSocketConnection.java:255)
    at de.tavendo.autobahn.WebSocketConnection.connect(WebSocketConnection.java:217)
    at anywheresoftware.b4a.objects.WebSocketWrapper.Connect(WebSocketWrapper.java:47)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:748)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
    at com.laransoft.riego.main_socket._connect(main_socket.java:203)
    at com.laransoft.riego.main_socket._reconnect_tick(main_socket.java:334)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

If I manually convert to ws://xn--maana-pta.com all works ok. Looking at websocket sources error is in mWsUri.getHost() function.

Is there any function to transform a punycode url into a "normal" url with B4x?

TIA!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub IDNToAscii(url As String) As String
   Dim jo As JavaObject
   Dim m As Matcher = Regex.Matcher("//([^\?]+)", url)
   If m.Find Then
     Dim a As String = jo.InitializeStatic("java.net.IDN").RunMethod("toASCII", Array(m.Group(1)))
     Return url.SubString2(0, m.GetStart(1)) & a & url.SubString(m.GetEnd(1))
   Else
     Return url
   End If
End Sub

Log(IDNToAscii("ws://mañana.com"))
 
Upvote 0

CGP

Member
Licensed User
Longtime User
Thanks @Erel! working in B4a and B4j. There is something for iOS ?

Just last night I was doing a port from javascript function :oops:
 
Upvote 0
Top