Using Socket.io client to connect to a different socket server - javascript

I have a simple server written in C. Its capable of receiving (and sending) messages via a specified socket. I'd like to use socket.io client to send messages to this server. I setup a simple html page and tried connecting via
var socket = io('http://my.server.ip:8080');
My server gets the connection but then socket.io gives this error repeatedly on the javascript console.
GET http://my.server.ip:8080/socket.io/?EIO=3&transport=polling&t=1431027762284-4 net::ERR_CONNECTION_RESET
I'm guessing my server should do a websocket 'handshake' to establish the connection. Is there a way to transmit a message in to a socket via JS with no handshake?
Please note my server does not use any standard, Its a rudimentary socket server that can receive and respond.

There are web socket server libraries you can use.
a plain socket does not work with web sockets.

Related

How to connect HTML WebSocket to C++ Socket

I have an app where I am trying to connect to a C++ server which opens up a socket.
// Client side
var ws = new WebSocket('ws://<<IP:PORT>>');
In the server side, it is opening up a socket using
int sockfd = socket(domain, type, protocol)
(ref: https://www.geeksforgeeks.org/socket-programming-cc/)
Not aware of the c++ server implementation.
I was told that the server would start a WebSocket server to which my HTML app can connect.
But, the connection is not happening. I feel that the WebSocket on the client side is not same as socket on the server side and hence the connection is not establishing.
Please, someone, suggest what is wrong and how to make it right.
P.S. Please don't mind if it is a dumb question.
Since the server guys are not ready to change their implementation of the plain socket, as a workaround I created a C++ client which connects to the server using the plain socket. Then I am creating a WebSocket Server using NodeJs which executes the C++ client as a child process to get the data. So, my WebSocket client is now able to get the data from the main server through the NodeJs WebSocket interface server.
Cons:
A dependency of the C++ client.
Latency (since network speed is not a factor now, it works).

Can javascript socket io listen unix sock?

Expected behavior
Server : unix://IP_ADDRESS/path/to/setup.sock
Client : javascript (cannot using node.js only can use socket.io or web socket)
Communicate server <-> client
Actual behavior
Web socket cannot listen unix://
I dont know how to listen or send message using socket.io
I'm so noob. Can anyone help me?
Unfortunately, web sockets won't be able to connect to AF_UNIX sockets, due to the nature of web sockets.
Looking at the RFC for web sockets they are
Conceptually, WebSocket is really just a layer on top of TCP [...]
Also, the web socket connection is initiated via HTTP. This means web sockets, as of now, are bound to TCP and I'm not sure what use case constrains you to Web Sockets instead of using regular local AF_UNIX sockets to access a local unix socket.
You should probably look into using the server to proxy between the unix socket and a web socket, if you have to.

WebSocket client and WinSock2 server, is it possible?

I have a c++ server using WinSock2 sockets,
can I connect to this server using JavaScript with WebSocket (as a client), or do I have to use WebSocket in both server and client? how can i do that?
can I connect to this server using JavaScript with WebSocket (as a client),
Yes.
or do I have to use WebSocket in both server and client?
No.
Sockets are just implementing an access layer to the IP stack.
How these are implemented at client or server side is irrelevant for establishing and using a (TCP/)IP connection.

Connect from plain javascript to socket.io backend? - Connection closed before handshake

I am trying to connect my client side javascript to a backend websocket service hosted using socket.io .
I am trying to use the native WebSocket object to connect to the socket.io server like :
new WebSocket("wss://localhost:8080/socket.io/websocket")
But I keep getting :
Connection closed before receiving a handshake response
I have tried connecting using the client side socket.io library just to confirm that the server is up and fine, and this works, but in production I only have the WebSocket object available.
Any ideas?
wss is for secure web socket, I assume you are not using ssl cerificate in localhost
so
new WebSocket("ws://localhost:8080/socket.io/websocket")
will work.
Edit:
When you use socket.io client, you will be using
var socket = io.connect('http://localhost:8080');
to use url with WebSocket you need ws://localhost:8080/socket.io/?transport=websocket
new WebSocket('ws://localhost:8080/socket.io/?transport=websocket')

Web socket client JavaScript?

i created TCP Client Using Web Socket 'JavaScript' and it can connect to C# TCP Server but i can't replay in C# to handshaking sent by JavaScript TCP Client
Please Help !
thanks.
There's no way we can help you here in any specific way without seeing your code on both client and server. webSocket is a protocol, not just a plain TCP connection. For your C# server to successfully accept a webSocket connection from any client, it must follow the entire webSocket protocol.
This reference Writing WebSocket Servers provides a pretty good summary of what a server has to do in order to successfully "speak" webSocket. Unless this is merely a learning exercise for you, you will probably want to get a library/class for C# that already implements a webSocket server as I'm sure there are many.
To give you a general idea, all webSocket connections start with an HTTP request that includes an "upgrade" header, a security key and a version. If the server agrees to the upgrade, then it responds with a security key. At that point, the two sides switch from HTTP to the webSocket protocol on that same socket and from then on, all data is then sent using the webSocket Frame format. This information is all outlined in the earlier MDN reference about creating webSocket servers.
Here is an article about writing webSockets servers in C#:
MDN: Writing a WebSocket server in C#

Categories

Resources