Reputation: 11
I have created a flask server using Python. My server is listening on 127.0.0.1.
I have created a SignalR client in C# and trying to connect to the above server. I am getting the following error on client side.
Error Opening Connection:Microsoft.AspNet.SignalR.Client.HttpClientException: StatusCode: 404, ReasonPhrase: 'NOT FOUND', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Date: Tue, 18 Jun 2019 04:11:55 GMT
Content-Type: text/html
Content-Length: 232
}
at Microsoft.AspNet.SignalR.Client.Http.DefaultHttpClient.<>c__DisplayClass5_0.b__1(HttpResponseMessage responseMessage) in //src/Microsoft.AspNet.SignalR.Client/Http/DefaultHttpClient.cs:line 95
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners2.<>c__DisplayClass3_0.<RunTask>b__0(Task
1 t) in //src/Microsoft.AspNet.SignalR.Core/TaskAsyncHelper.cs:line 1280
Unhandled Exception: System.InvalidOperationException: Data cannot be sent because the connection is in the disconnected state. Call start before sending any data. at Microsoft.AspNet.SignalR.Client.Connection.Send(String data) in //src/Microsoft.AspNet.SignalR.Client/Connection.cs:line 815 at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[TResult,TProgress](String method, Action`1 onProgress, Object[] args) in //src/Microsoft.AspNet.SignalR.Client/Hubs/HubProxy.cs:line 184 at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke(String method, Object[] args) in /_/src/Microsoft.AspNet.SignalR.Client/Hubs/HubProxy.cs:line 71
On server side I see: 127.0.0.1 - - [2019-06-18 09:41:55] "GET /signalr/negotiate?clientProtocol=2.1&connectionData=[%7B%22Name%22:%22main%22%7D] HTTP/1.1" 404
When I connect from IE browser, it is working fine.
hubConnection = new HubConnection("http://127.0.0.1:5000/", true);
IHubProxy myHub = hubConnection.CreateHubProxy("main");
hubConnection.Start(new LongPollingTransport()).ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Error Opening Connection:{0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Connected");
}
}).Wait();
Can somebody tell me if it is actually possible to connect from SignalR client to a flask server? If so, what has to be corrected?
Upvotes: 1
Views: 988
Reputation: 1
I was able to do something like this indirectly using SSE to get the SignalR message to Flask (caveat emptor: this is not production code it is only to do a POC):
Python:
from queue import Queue
from threading import Thread
messageQueue = Queue()
def myprint(*values: object):
messageQueue.put(values)
So from the example in #3 above in the function eventStream() instead of just "Poll data from the database" you would have:
def eventStream():
while True:
# Check concurrent queue
messages = messageQueue.get()
yield "data: {}\n\n".format(messages)
And in my html I have something like this where I use a Bootstrap toast-popup (https://getbootstrap.com/docs/5.0/components/toasts/) to show the message came in to the browser:
<script>
$(document).ready(function(){
// From https://medium.com/code-zen/python-generator-and-html-server-sent-events-3cdf14140e56
var toastWorking = document.getElementById('toast-popup');
var eventSource = new EventSource("/stream");
eventSource.onmessage = function(e) {
var toast = new bootstrap.Toast(toastWorking)
var today = new Date();
document.getElementById("toast-popup-small-display").innerHTML = today.toLocaleTimeString(); //today.toLocaleDateString("en-US", options)
document.getElementById("toast-popup-message").innerHTML = e.data;
//alert(e.data)
toast.show('show');
};
</script>
Good luck
Upvotes: 0