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#
Related
I have a websocket client and I want it to send a ping frame to my WS server.
According to the RFC 6455 a ping frame is represented by opcode %x9 but I don't know if it's even possible to send from a browser.
Any help will be appreciated
Building on-top of the comments in your original post, you can manually send pings via websocket.
The link by Daniel W. of the ws library has a ping() method which can be called from a valid websocket client. See an example here.
If you have written your own websocket library then you must conform to the standard framing outlined in RFC6455 The Websocket Protocol.
You should be able to pack a buffer with the correct header and control opcode and send it over the upgraded HTTP1.1 TCP connection.
Edit:
Further research shows, at the point of writing, the Ping method is not supported via the native Websocket Client and the send method seems to only support payloads and not command messages, which include opcodes for commands such as ping.
An option could be to go with a WASM based solution which could make non-XHR or non-fetch based TCP connections, however this comes with a whole different set of challenges.
I have a java client and I need to rewrite it in (client-side) javascript.
I open the java Socket like this:
Socket socket = new Socket("127.0.0.1", 5015);
So I tried to use websocket in javascript:
let socket = new WebSocket("http://127.0.0.1:5015");
but here I have a js error:
Uncaught DOMException: Failed to construct 'WebSocket':
The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed.
I tried also to use the 'ws' or 'wss' protocol but the server didn't want to handshake with such protocols.
Is there a way to make such socket connection in client-side javascript or it's definitely prohibited?
The answer is a little more complicated than "no you can't do it".
Javascript in a regular web page running in a web browser cannot open a plain socket. The fundamental reason is that it is a security risk for the user. So it is intentionally not allowed.
WebSockets are the secure way to do this. In conjunction with other browser security mechanisms, they limit what a web page is permitted to connect to.
However, that is not the end of the story. It is possible (at least in theory) for trusted code to send and receive TCP and UDP traffic. The problem is that the APIs for doing this are non-standard (e.g. browser specific). In some cases are themselves implemented as 3rd-party browser extensions.
So if you really wanted to pursue this for you application, you are going to have to distribute your code as a trusted browser plugin / extension AND deal with a range of browser portability issues.
It is worth noting that there was a W3C Working Group that was trying to standardize raw socket APIs, but they have officially abandoned their efforts. Their last working draft can be found at:
https://www.w3.org/TR/tcp-udp-sockets/
Finally, there is the problem that a trusted browser extension / plugin requires the user's consent to install. Getting informed consent for something like this is difficult, given the deep and subtle security issues associated with embedding this kind of functionality in the user's browser.
No, you can't make an arbitrary TCP connection from a web page in any browser.
Web Sockets are fundamentally different than TCP sockets... they're essentially unrelated. They're a thin layer on top of HTTP along with a client API which allows bidirectional communication between a Web Socket client and a server supporting Web Sockets.
There are proxy servers you can run that allow connecting through them to make TCP connections, but this of course is a server feature and not something you can do in-browser alone.
The opening handshake is intended to be compatible with HTTP-based
server-side software and intermediaries, so that a single port can be
used by both HTTP clients talking to that server and WebSocket
clients talking to that server. To this end, the WebSocket client's
handshake is an HTTP Upgrade request:
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
https://www.rfc-editor.org/rfc/rfc6455
WebSockets server must be able to handle HTTP requests!
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).
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.
I am using MongooseIM as my XMPP server, and this server typically use TCP as transport. This server works fine with my iOS client. But when I want to create the same service in web app, I found out that it does not work with any kind of javascript XMPP framework.
Because with browser, we cannot use TCP as transport. Instead, the alternatives are WebSocket and BOSH. When I use WebSocket with MongooseIM, it shows hand shake failed . While with BOSH, it shows 403 error. So does MongooseIM really work with WebSocket or BOSH?
And also, with TCP as my transport, the connection url is 'example.org', why it would be 'example.org/http-bind' with BOSH? Why is there the difference between 2 transport?
Pure XMPP TCP connection, BOSH and Websockets are quite different protocols. Both BOSH and Websockets use separate suffix (http-bind, ws-xmpp) to distinguish the endpoints if they are running on the same port. It spares the server some guessing what protocol is actually client going to use and provides nice separation.
What URL do you exactly use for BOSH and Websockets connection? In the former case it should be something like http://localhost:5280/http-bind and in the latter ws://localhost:5280/ws-xmpp.
What JS clients have you tried?