Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  175] [ 7]  / answers: 1 / hits: 32530  / 9 Years ago, wed, december 9, 2015, 12:00:00

I am trying to create a simple program to send a message to a socket in plain text. The sender is a webpage in javascript using websocket and the server is a C program. I don't have any control over the C code but I have been assured by the codes owners that I can use simple javascript to send the message. My code is as follows:



<!DOCTYPE HTML>
<html>
<head>
<script type=text/javascript>
//initialize the websocket ws
var ws;
//Open the socket connection
function openSock() {
//test for Websocket compatibility in the browser
if (WebSocket in window) {
// log socket attempt
console.log(Attempting to open socket!);
//open web socket ws
ws = new WebSocket(ws://192.168.6.222:11000/echo);
} else { // else the browser doesn't support WebSocket
//Websocket is not supported
alert(APS NOT supported by your Browser!);
}
}
//send the command to socket ws
function sendCommand() {
console.log(Attempting to Send Message);
ws.send(your message here);
}
//close the socket ws
function closeSock() {
console.log(Closing Socket);
// close socket ws
ws.close();
}

</script>
</head>
<body>
<div id=sse>
<a href=javascript:openSock()><input type=submit value=open></a>
<a href=javascript:sendCommand()><input type=submit value=send></a>
<a href=javascript:closeSock()><input type=submit value=close></a>
</div>
</body>
</html>


When I click the connect button I get this error:




WebSocket connection to 'ws://192.168.6.222:11000/echo' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET




I have checked the firewall and the port seems to be open. After spending a fair amount of time troubleshooting the server (making sure it is running and on the network, etc.) I am wondering if I did something wrong with this client side code.



From my limited knowledge and the reading I have done this looks correct but any help is welcome.


More From » websocket

 Answers
10

How does the server side of the connection look like?
Maybe you are using a faulty WebSocket Lib which does not provide a valid handshake.



Because when testing the client against ws://echo.websocket.org everything seems to work perfectly fine. This strongly suggests that the websocket client is not source of the problem.


[#64118] Monday, December 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;