Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a socket via JavaScript (without flash)

I have a browser based app that needs to communicate with another service running on the client machine via a socket connection from the browser using JavaScript.

I need to post and parse XML back and forth on the socket.

I am unable to go down the flash path as the cross domain security is a barrier, ie the service running on the socket is not able to be modified to support Flash's crossdomain security.

What are my options for a pure JS based solution?

like image 313
Vaughan Avatar asked May 13 '26 07:05

Vaughan


1 Answers

You've got two major problems here:

  1. It's difficult in javascript to access non HTTP resources,
  2. It's difficult in javascript to access resources not loaded from the same server.

There are exceptions to both of these, but the conjunction of exceptions available might not exactly match with what you need. Here are some possibilities:

  1. Some sort of proxy on your own server that connects back to the machine with the XML service on behalf of your web app.

  2. If you can control the client machine somewhat you can run a server on it that can embed the XML in a JSONP formatted http response, you can access by adding simple script tags, and send messages the other way by using a script tag to request a url with your data encoded into it.

  3. If when you say 'socket' you're referring to an HTTP connection, then there are a number of options, one is to add a Access-Control-Allow-Origin header to the HTTP, then you can do gets and posts using normal XMLHttpRequests in recent browsers.

like image 139
kybernetikos Avatar answered May 15 '26 21:05

kybernetikos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!