Hi,
I am trying to connect from Xamarin but I am getting a timeout connection error.
Any idea of what is wrong?
I am using System.Net.WebSockets with the following code:
==============================================================================
module WebsocketChannels
open Fable.Remoting.DotnetClient
open System.Threading
open System.Threading.Tasks
open System.Linq
open Shared
type MessageType =
| TextMsg
| ImageMsg
| JsonMsg
type ServerMessage =
{ Time : DateTime
From: Guid option
Type: MessageType
Message : string }
type ClientMessage =
{
ClientId: int option
To: Guid option
Message: string
}
// Add more messages that can go from server -> client here...
type WebSocketServerMessage =
| BroadcastMessage of ServerMessage
// Add more message that can go from client -> server here...
type WebSocketClientMessage =
| TextMessage of ClientMessage
| ImageMessage of ClientMessage
/// Status of the websocket.
type WsSender = WebSocketClientMessage -> unit
//type BroadcastMode = ViaWebSocket | ViaHTTP
type ConnectionState =
| DisconnectedFromServer | ConnectedToServer of WsSender | Connecting
member this.IsConnected =
match this with
| ConnectedToServer _ -> true
| DisconnectedFromServer | Connecting -> false
type Msg =
| ReceivedFromServer of WebSocketServerMessage
| ConnectionChange of ConnectionState
| ConnectionError of exn
module Channel =
open Newtonsoft.Json
open System.Net.WebSockets
open Fabulous
open System
let cts = new CancellationTokenSource()
let inline decode<'a> x = x |> unbox<string> |> JsonConvert.DeserializeObject<'a>
let buildWsSender (ws:WebSocket) : WsSender =
fun (message:WebSocketClientMessage) ->
let message = {| Topic = ""; Ref = ""; Payload = message |}
let message = JsonConvert.SerializeObject(message)
let byteMessage = System.Text.Encoding.UTF8.GetBytes(message)
let segmnet = new ArraySegment<byte>(byteMessage)
ws.SendAsync(segmnet, WebSocketMessageType.Text, true, cts.Token).Wait()
let subscription f _ =
let sub dispatch =
let cts = new CancellationTokenSource()
let mutable ws = new ClientWebSocket()
/// Continually tries to connect to the server websocket.
let connect () =
Task.Factory.StartNew(fun () ->
while true do
try
if ws.State <> WebSocketState.Open
then ws.ConnectAsync(new Uri("ws://10.0.1.3:8085"), cts.Token).Wait()
dispatch (f (ConnectionChange (ConnectedToServer (buildWsSender ws))))
let message = new ArraySegment<byte>(Array.zeroCreate 4096)
let mutable foo = true
while foo do
let result = ws.ReceiveAsync(message, cts.Token).Result
let messageBytes = message.Skip(message.Offset).Take(result.Count).ToArray()
let serialisedMessage = System.Text.Encoding.UTF8.GetString(messageBytes)
try
let msg = decode<WebSocketServerMessage>(serialisedMessage)
dispatch (f (ReceivedFromServer msg))
foo <- false
with | e ->
dispatch (f (ConnectionError e))
Thread.Sleep 5000
with e -> dispatch (f (ConnectionError e))
ws.Dispose()
Console.WriteLine e.StackTrace
ws <- new ClientWebSocket()
Thread.Sleep 5000
,cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Start()
connect()
Cmd.ofSub sub