Universal Electricity
Universal Electricity

Reputation: 775

Connecting to a ServerSocket in Java with a Javascript client

If I have a ServerSocket in Java, how can I connect to it through Javascript? This isn't a WebSocket. Here is part of my server code:

ServerSocket server = new ServerSocket(3000);
Socket client = server.accept();
BufferedWriter clientOut = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
clientOut.write('Hello World!\n');
clientOut.flush();
clientOut.close();
client.close();
server.close();

When I try to use a WebSocket to connect, I get this error:

SyntaxError: An invalid or illegal string was specified

And this is what I use to connect:

socket = new WebSocket('http://localhost:3000')

Do note that I would also like a pure Javascript way of doing this, and I wouldn't want a Java applet hidden inside my page to send and read data through the socket.

Upvotes: 2

Views: 849

Answers (1)

happymeal
happymeal

Reputation: 1413

the javascript WebSocket is different from the generic socket. if you want to use WebSockets, your java server must implement the WS/WSS handshake protocols.

there is no generic socket connections for javascript. i would suggest that you create a corresponding java socket client.

Upvotes: 1

Related Questions