Reputation: 63
So, I am trying to build a C application which functions as a Websockets Server.There are many related questions on here, but none of them seem to be able to help with the problem. The program is able to initially establish a connection, complete the handshake for the websocket successfully, however, cannot seem to be able to keep the connection open. On the client side, I get the error
WebSocket.js:7605 Uncaught Error: Attempt to send message on unopened or closed WebSocket at _8b2.window.WebSocket._8d2.send (WebSocket.js:7605) at (index):34
whenever I try to use the send() function on the websocket connection on the client side. And also it gives me the error
WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established.
Here is the source code of the server:
int new_socket;
int activity;
int i;
int sd;
int max_sd;
int bytes_read;
fd_set readfds;
int master_socket;
noPollConn* new_conn;
/*Create no poll context*/
noPollCtx* ctx = nopoll_ctx_new();
noPollConn* conn;
char buffer[3000];
/*Create a connection listener at 127.0.0.1 (loopback) on port 5959*/
noPollConn * listener = nopoll_listener_new(ctx, "127.0.0.1","5959");
/*Get the socket of the lister*/
master_socket = nopoll_conn_socket(listener);
if(!nopoll_conn_is_ok(listener)) {
perror("There was an error creating the listener!\n");
exit(EXIT_FAILURE);
}
puts("Waiting for connections ...");
while(TRUE){
printf("Start of the while loop\n");
FD_ZERO(&readfds);
FD_SET(master_socket, &readfds);
max_sd = master_socket;
printf("The number of connections is %i\n",cons);
for (i = 0 ; i < cons ; i++) {
sd = nopoll_conn_socket((noPollConn*)vector_get(clients,i));
if(sd > 0)
FD_SET(sd,&readfds);
if(sd > max_sd)
max_sd = sd;
}
printf("The max fd is %i\n",max_sd);
activity = select(max_sd + 1 , &readfds , NULL , NULL , NULL);
if ((activity < 0) && (errno!=EINTR)) {
puts("select error");
exit(EXIT_FAILURE);
}
if (FD_ISSET(master_socket, &readfds)){
new_conn = nopoll_conn_accept(ctx,listener);
puts("Waiting for the connection to be ready");
nopoll_conn_is_ready(conn);
/*Vector is actually a doubly linked list*/
vector_push(&clients,new_conn);
cons++;
}
/*TODO: Implement disconnect*/
for (i = 0; i < cons; i++){
printf("Checking on user %i\n",i);
conn = (noPollConn*)vector_get(clients,i);
sd = nopoll_conn_socket(conn);
if (FD_ISSET(sd, &readfds)){
printf("Receiving info from socket no. %d...\n",sd);
bytes_read = recv(sd,buffer,4000,MSG_DONTWAIT);
buffer[bytes_read] = '\0';
printf("Received the msg --> %s\n",buffer);
}
}
memset(buffer,0,3000);
}
Just a warning though, this code does go in an infinite loop right now as I have not implemented disconnection from the client on the server side.
For the client
<pre><code>
var connection = new WebSocket('ws://127.0.0.1:5959');
connection.onopen = function(){
connection.send("TEST");
alert("Connected");
}
connection.onerror = function(error){
console.log('Error detected: ' + error);
}
connection.onmessage = function (event) {
alert(event.data);
}
connection.close();
</code></pre>
Perhaps I am missing something very important? I have gone through many tutorials, and I can't seem to figure out what the issue is. Any help is greatly appreciated!
Calling .send()
in the .onopen()
function results in the error:
WebSocket connection to 'ws://127.0.0.1:5959/?.kl=Y' failed: WebSocket is closed before the connection is established. UPDATE
The error is on the server side. It might be related to me handling the connection as though it were a socket in some regards in the C code.
Upvotes: 0
Views: 4861
Reputation: 1
If no errors are logged at server, the issue could be call to .send()
before open
event occurs. Call .send()
within open
event handler. Else, the issue is at server.
const socket = new WebSocket("ws://echo.websocket.org/");
socket.onopen = function(e) {
socket.send("WebSocket rocks");
console.log("self.socket event.type:", e.type);
};
socket.onmessage = function(e) {
console.log(e.data);
};
socket.onerror = function(e) {
console.log("self.socket error", e);
};
socket.onclose = function(e) {
console.log("self.socket event.type", e.type);
};
See How do I test my WebSocket which is developed in JavaScript
plnkr http://plnkr.co/edit/W8Wgyw0mbxdMkMMe4wg4?p=preview
Upvotes: 1