Check telnet connection with server IP and Port number in JScript - javascript

I need to test telnet connection to the server in my local pc. My pc don't have any server side languages like PHP, NodeJS,.. I want to do this in basic JavaScript or chrome extension from latest chrome. I have checked with websocket HTML5. But, it was not working. Here's my code,
<script>
var connection = new WebSocket('ws://10.0.30.1:80');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
</script>
Lets explain what's my mistake and give me a solution if any other possible.

You can't. Unless you set up a proxy server, a websocket can only communicate with a websocket server.

Related

Sending bytes from javascript client to java backend DatagramSocket

I have a java app that functions as a server, listening to some port using the java.net.DatagramSocket class. The server is able to receive packets from some other java clients.
What I did not manage to do is have a javascript client to the server.
I've tried sending messages as WebSocket client
const ws = new WebSocket("ws://localhost:9999");
ws.addEventListener("open", () => {
console.log("CONNECTED")
})
But I'm getting the following error in the browser console:
App.js:9 WebSocket connection to 'ws://localhost:9999/' failed:
I've also tried using net library to send message:
const net = require("net");
const options = {
post: 9999
};
const client = net.createConnection(options);
client.write("TEST")
But I'm getting the following error:
Uncaught TypeError: net.createConnection is not a function
Is there any other way to do this?
UDP (DatagramSocket) and WebSocket are totally different things.
WebSocket is a protocol on top of HTTP which is on top of TCP.
In JavaScript running inside the browser you cannot send UDP packages.
You can write some kind of a proxy server, which can receive messages via WebSocket from your JavaScript program and send them via UDP to your server.
Or extend your server with a WebSocket interface.

Netcat over Javascript

I'm working under one cloud solution, that's allow user to print receipts on ESC/POS printer. So it's actually pretty easy to print on it like
echo "Hello world!" | nc 192.168.1.37 9100
But, I need to do the same with from user's browser. So i've tried like this:
var connection = new WebSocket('ws://IPAddress:Port');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
And almost done, but WS send whole stack of HTTP headers starts with
GET / HTTP/1.1
...
Is there way to send it without headers? Or other way to send data to printer?
netcat or nc use TCP protocol and it doesn't use header like "POST /1 HTTP/1.1\r\n" like http requests do. You are probably thinking about using net.Socket() and not WebSocket
I do not want to confuse you because you aim to use javascript in browser, but i used net.Socket() successfully with node.js. The point of this comment is, that you aim to establish TCP connection and not WS.
I think that HTTP always starts sending GET and HTTP Headers.
Maybe you can use some old technology like signed Java Applets or Flash.
In addition, you can download a binary file to the client PC to communicate.

Websocket Server, based on socketo.me not accepting connections from shared internet

I managed to configure a simple websocket server according to this tutorial in AWS EC2 instance and its working fine.
But only from my home internet connection which has a real IP and told as a dedicated internet line.
I tried with a very simple javascript example code from client side (using a HTML page) and it works perfectly if I use that dedicated internet connection from my PC/Mac. (I used autobahn.min.js) above the following script.
var conn = new ab.Session('ws://X.X.X.X:8080',
function() {
console.log("Connection established!");
// To Do: Subscribe with client ID
},
function() {
console.warn('Connection closed!');
},
{'skipSubprotocolCheck': false}
);
but it fails if I run the same simple file/script from under another shared internet connection such as cellular data or something like that. I get the following error in browser console.
WebSocket connection to 'ws://X.X.X.X:8080/ws' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT example.com Connection closed!
The server is in AWS EC2 instance. Yes, 8080 is enabled under security group. Actually all works fine except client connection goes from some specific types of internet connection based computer.
Thanks in advance for any help!

Faye and Nodejs Issue

Hi currently I'm trying to do a notification feature on a webapp and I decided to use Faye in order to make them live notifications. Now the feature works fine on my local computer, but when somebody else tries to connect to my network, they get a connection refused and faye doesn't work.
Current code working on my local machine:
var http = require('http');
var faye = require('faye');
var server = http.createServer();
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
bayeux.attach(server);
server.listen(8000);
I managed to read some other questions related to this issue, and people said to make the server listen to all interfaces by adding: '0.0.0.0' to the server.listen call. When I do this it even stops working on my local machine. Any help will be appreciated!, Nodejs is not the only server running on my machine but the port is open so thats not an issue. I get a get failed request towards the faye message request.

Chrome websockets, readystate always on 0

I'm trying to use websockets on a website, so first I developed a small and very simple websocket page just to test it.
I have a websocket server running on my localhost, it is based on the python Tornado "Chat" demo. For some reason the chat demo app runs perfectly but I can't seem to use the websocket with my own page although some form of connection is made.
I am testing this using the latest Chromium version, so implementing websockets version 13, which is supported by the Tornado implementation.
So here is the problem:
Load page, js executes and Upgrade request is sent to the server
Server receives request and answers
So here in my understanding Chrome should set readyState = 1 and I should be able to send messages from my page.
Only for some reason it doesn't work, readyState remains 0 and of course if I try to send a message I receive an INVALID_STATE_ERR.
Here are the Headers :
Request:
GET ws://127.0.0.1:8000/chatsocket HTTP/1.1
Origin: http://127.0.0.1
Cookie: _xsrf=9f73731fc2d544df864ce777bef0775a
Connection: Upgrade
Host: 127.0.0.1:8000
Sec-WebSocket-Key: pkwlpY+TtxfgUrm3M4WtTQ==
Upgrade: websocket
Sec-WebSocket-Version: 13
Response:
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Accept: ur9KL2jBhYB38e2SgwOkjyBlQXk=
Any help is appreciated :)
--- EDIT ---
So I figured it out in the end, if you run into the same problem here is the reason:
WebSocket's readyState is updated when the Thread ENDS !
So running a code like:
var ws = new WebSocket(stuff);
while(ws.readyState==0){};
Will send the browser in an infinite loop...
Running code like:
var ws=new WebSocket(stuff);
do_other_stuf();
Might work, but you wont be able to use WS.
If the code that is supposed to run after the socket opens uses the socket this is the way it will have to be written:
var ws=new WebSocket(stuff);
ws.onopen = new function(){
// some code that need WS to be open
}
// rest of the code that doesn't require WS to be open
A better way to do this would be to let the thread end by using an asynchronous call:
var ws = new WebSocket(stuff);
setTimeout(whatever,500);
function whatever(){
if(ws.readyState==1){
// The code you want to run after WS is open
}
else
setTimeout(whatever,500);
}
In order to trigger some functionality when WebSockets are connected please use callbacks:
var socket = new WebSocket(...);
socket.onopen = function() {
// socket is connected
};
socket.onerror = function() {
// some error happened
};
socket.onmessage = function(evt) {
// you get a message: evt.data
};
Using readyState is well bad thing to do in such case, especially if you have to ask it multiple times (is just unnecessary).
Additionally take in account that each vendor of browsers implements WebSockets with very slight differences (unfortunately), so make sure you test it in most browsers.

Categories

Resources