Andreas Schnieders
Andreas Schnieders

Reputation: 43

Java Application to "forward" data from TCPSocket to WebSocket?

I'm slightly confused, hope someone has some hints for me.

I need to get some data from a TCPSocket (which I can not modify) to an HTML5/JavaScript-Website "running" on an iPad.

Few things, I think I understood:
- JavaScript does not support plain TCPSockets, neither does HTML5
- JavaScript does support WebSockets (var socket = new WebSocket...) o_O
- Java natively does not support WebSockets
- Every edge of the web recommends "jWebSocket" for Java WebSocket implementations
- Still jWebSocket is not something like a lib I use in my Java-Application say in very raw pseudocode like this:

ClientSocket tcpSocket = new ClientSocket(whatever);

WebSocket webSocket = new WebSocket(whatever);

tcpSocket.connectToTCPServer();
webSocket.acceptClientConnection();

data = tcpSocket.receive();

processedData = processData(data);

webSocket.send(processedData);

Right?

WHAT for the love of god then is the shortest way to implement something like presented above? Where is this easypeasy Web 2.0 communication things the whole web talks about? I do not honestly have to download and build that whole jWebSocket-Maven-Projekt, do I?

Many thanks in advance, Andreas

Upvotes: 3

Views: 1209

Answers (1)

kanaka
kanaka

Reputation: 73197

If Java is not in fact a hard requirement, then a simple solution would be to use websockify which is designed for exactly the case of proxying/bridging between WebSockets and raw TCP sockets. Websockify is a simple python server.

One of the challenges that websockify addresses is that many browser implementations in the wild do not yet support the WebSocket API changes to support binary data (and if you are connecting to a regular TCP server then you likely need to be able to send/receive binary data). Websockify gets around this by base64 encoding/decoding data to and from the WebSocket client. The included include/websock.js makes sending and receiving binary data (as a normal Javascript array of bytes). See the websock.js API.

Disclaimer: I made websockify.

Upvotes: 1

Related Questions