Async Websocket to Javascript

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

Async Websocket to Javascript

Postby CZauX » Fri Apr 15, 2016 12:28 pm

I'm looking to spin up WebsocketSharp as a websocket server that will then send messages to javascript that is located in a browser instance/tab.

I am able to add threaded functionality to OnLoadEnd, however that is blocking, and a simple async function doesn't work well. I'm guessing I have to spawn a thread somewhere in the programs initialization, but CEF and CefGlue have a lot of odd classes to integrate with and I can't seem to find any examples or classes that would accomplish what I'm looking for.

Any help is appreciated, code snippets especially. Thank you for your time.
CZauX
Newbie
 
Posts: 7
Joined: Fri Apr 15, 2016 12:20 pm

Re: Async Websocket to Javascript

Postby fddima » Fri Apr 15, 2016 2:35 pm

It is not clear actually what you trying to do. Websocket is just TCP server with HTTP handshake, nothing more. If you own all parts of code, then you should do IPC without HTTP/webseckets. If not - then it depends.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: Async Websocket to Javascript

Postby CZauX » Fri Apr 15, 2016 5:32 pm

fddima wrote:It is not clear actually what you trying to do. Websocket is just TCP server with HTTP handshake, nothing more. If you own all parts of code, then you should do IPC without HTTP/webseckets. If not - then it depends.


Even if I was doing IPC, i would still need to run something async/seperate from the browser thread to receive data from other sources and send it to Javascript.

In this case, I don't have control of all parts of the code, and am limited to using Websockets.

What I am trying to do is send/receive messages via a websocket in an asynchronous manner, messages are sent to javascript to render. My application acts
as a server end point, not a client end point.
CZauX
Newbie
 
Posts: 7
Joined: Fri Apr 15, 2016 12:20 pm

Re: Async Websocket to Javascript

Postby fddima » Sat Apr 16, 2016 4:23 am

Clear. So original question has been around thread. What's wrong new Thread.Start?
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: Async Websocket to Javascript

Postby CZauX » Sun Apr 17, 2016 10:26 pm

fddima wrote:Clear. So original question has been around thread. What's wrong new Thread.Start?


Using Thread.Start is a blocking operation to the chromium rendering. I can spin up another thread on something like OnLoadEnd, but the problem becomes that unless I make the thread sleep, the main rendering stops. Since the Websocket is a server, I can't just put it to sleep.

The second problem with threading is that if I spin up another thread, it needs to be able to communicate with the browser instance to execute Javascript, so I have to have the thread executing in a specific location, or some other method of communication, and thats where I'm lost at. This seems to be more of an experience with CefGlue issue than a general C# problem, unless I'm majorly overlooking something.
CZauX
Newbie
 
Posts: 7
Joined: Fri Apr 15, 2016 12:20 pm

Re: Async Websocket to Javascript

Postby fddima » Mon Apr 18, 2016 12:14 am

CZauX wrote:Using Thread.Start is a blocking operation to the chromium rendering. I can spin up another thread on something like OnLoadEnd, but the problem becomes that unless I make the thread sleep, the main rendering stops. Since the Websocket is a server, I can't just put it to sleep.

The second problem with threading is that if I spin up another thread, it needs to be able to communicate with the browser instance to execute Javascript, so I have to have the thread executing in a specific location, or some other method of communication, and thats where I'm lost at. This seems to be more of an experience with CefGlue issue than a general C# problem, unless I'm majorly overlooking something.


Sorry, I'm far from you problem, and it is hard to understand without knowledge what exactly happend.

fddima wrote:Chromium in case of websokets will initiate HTTP handshake which you can spoof via CEF request handler. Then, if request hit you, you can allow to upgrade connection (and it will be no more HTTP), or reject connection upgrade, and in this case some http-based fallback should be used (but I'm not sure, need to play around or read some docs), but fallback should be used in case for pure HTTP proxies (but again needs to be checked). So, just for idea, if you can't change client code, may be easier 'take a look of dumb' and try to handle this over http (request handler),

Update: OnBeforeResourceLoad will not be called for WS HTTP handshake, but scheme handler factory will be used.

or may be client code already can fallback to other strategy (XHR/long polling), which can be easily handled (again via request handler).

Things about Thread.Start... It is not blocking. You can spawn new threads many as you want. And it is bit hard to understand what problems with IPC. All of them are async, and if you need sync IPC - then you should implement own, but they usually bad because of blocking. ExecuteJavaScript method on other side is async, as any IPC with renderer. So actually I'm feel that you do something with js, but it is unclear how it connected to websockets. Anyway if you simulate somehow ws events - all of them are async. But WS is networking/channel, so again unclear how it connected to JS.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: Async Websocket to Javascript

Postby CZauX » Tue Apr 19, 2016 6:54 pm

fddima wrote:
CZauX wrote:Using Thread.Start is a blocking operation to the chromium rendering. I can spin up another thread on something like OnLoadEnd, but the problem becomes that unless I make the thread sleep, the main rendering stops. Since the Websocket is a server, I can't just put it to sleep.

The second problem with threading is that if I spin up another thread, it needs to be able to communicate with the browser instance to execute Javascript, so I have to have the thread executing in a specific location, or some other method of communication, and thats where I'm lost at. This seems to be more of an experience with CefGlue issue than a general C# problem, unless I'm majorly overlooking something.


Sorry, I'm far from you problem, and it is hard to understand without knowledge what exactly happend.

fddima wrote:Chromium in case of websokets will initiate HTTP handshake which you can spoof via CEF request handler. Then, if request hit you, you can allow to upgrade connection (and it will be no more HTTP), or reject connection upgrade, and in this case some http-based fallback should be used (but I'm not sure, need to play around or read some docs), but fallback should be used in case for pure HTTP proxies (but again needs to be checked). So, just for idea, if you can't change client code, may be easier 'take a look of dumb' and try to handle this over http (request handler),

Update: OnBeforeResourceLoad will not be called for WS HTTP handshake, but scheme handler factory will be used.

or may be client code already can fallback to other strategy (XHR/long polling), which can be easily handled (again via request handler).

Things about Thread.Start... It is not blocking. You can spawn new threads many as you want. And it is bit hard to understand what problems with IPC. All of them are async, and if you need sync IPC - then you should implement own, but they usually bad because of blocking. ExecuteJavaScript method on other side is async, as any IPC with renderer. So actually I'm feel that you do something with js, but it is unclear how it connected to websockets. Anyway if you simulate somehow ws events - all of them are async. But WS is networking/channel, so again unclear how it connected to JS.


Browser JS cannot be used as a Websocket Server, only a client. I already have websocket-sharp setup as a websocket server, the problem just is how to integrate something event-based like that to the threading of CefGlue and be apart of the same instance of chromium to send it JS.
CZauX
Newbie
 
Posts: 7
Joined: Fri Apr 15, 2016 12:20 pm

Re: Async Websocket to Javascript

Postby fddima » Tue Apr 19, 2016 10:13 pm

CZauX wrote:Browser JS cannot be used as a Websocket Server, only a client. I already have websocket-sharp setup as a websocket server, the problem just is how to integrate something event-based like that to the threading of CefGlue and be apart of the same instance of chromium to send it JS.


You still not describe anything what exactly describes you problem. CEF and CefGlue does not force you in specific threading model. Actually CefGlue can be used together with async/await code without issues. You always can post tasks into specific CEF thread via API, if it is needed.

You should start server:
If it is blocking, then start it in own thread, I.e. non UI, non CEF. If it is async then start it on any thread (assuming that default scheduler is used). If it is expect to execute own messages on to specific threads, then it also should be used on own thread or take a look how it is can be customized. Thread.Start can help.

Client connects to server by URL, so nothing additional steps needed. Assuming that client connects to local host.

Why you need post anything to JavaScript is completely unclear, because WebSocket already provided by browser.

If you trying to replace standard client to proxy WS connections to own WS server, then it is hardly other question, but it is again simple doable via CEF API.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 16 guests