Comments (4)
ok, its done, was quite easy.
- inserted the new opcode in ws.h
- in ws.c I copied ws_sendframe to ws_sendframe_binary and made the required changes
- added a line in the html example
I do not use the receive function, but I think it will need to read the length from the frame.
ws.h:
/* Frame types. */
#define WS_FR_OP_TXT 1
#define WS_FR_OP_BINARY 2
#define WS_FR_OP_CLSE 8
ws.c:
/**
* Creates and send an WebSocket frame
* with some binary message.
* @param fd Target to be send.
* @param msg Message to be send.
*/
int ws_sendframe_binary(int fd, unsigned char *msg, uint64_t length)
{
unsigned char *response; /* Response data. */
unsigned char frame[10]; /* Frame. */
uint8_t idx_first_rData; /* Index data. */
int idx_response; /* Index response. */
int output; /* Bytes sent. */
/* Binary data. */
frame[0] = (WS_FIN | WS_FR_OP_BINARY);
/* Split the size between octects. */
if (length <= 125)
{
frame[1] = length & 0x7F;
idx_first_rData = 2;
}
/* Size between 126 and 65535 bytes. */
else if (length >= 126 && length <= 65535)
{
frame[1] = 126;
frame[2] = (length >> 8) & 255;
frame[3] = length & 255;
idx_first_rData = 4;
}
/* More than 65535 bytes. */
else
{
frame[1] = 127;
frame[2] = (unsigned char) ((length >> 56) & 255);
frame[3] = (unsigned char) ((length >> 48) & 255);
frame[4] = (unsigned char) ((length >> 40) & 255);
frame[5] = (unsigned char) ((length >> 32) & 255);
frame[6] = (unsigned char) ((length >> 24) & 255);
frame[7] = (unsigned char) ((length >> 16) & 255);
frame[8] = (unsigned char) ((length >> 8) & 255);
frame[9] = (unsigned char) (length & 255);
idx_first_rData = 10;
}
/* Add frame bytes. */
idx_response = 0;
response = malloc( sizeof(unsigned char) * (idx_first_rData + length + 1) );
for (int i = 0; i < idx_first_rData; i++)
{
response[i] = frame[i];
idx_response++;
}
/* Add data bytes. */
for (int i = 0; i < length; i++)
{
response[idx_response] = msg[i];
idx_response++;
}
output = write(fd, response, idx_response);
free(response);
return (output);
}
example:
/* Do connection. */
ws = new WebSocket("ws://" + addr + ":8080");
ws.binaryType = "arraybuffer";
from wsserver.
Thank you @dj0abr for the tip, I'm glad that you like the project, means a lot to me. I've already pushed a commit including the <stdint.h> header.
Regarding your question, there's 2 years that I have made this library, so I have to revisit the concepts before to give you a proper answer, but I think that it's not hard at all. In the last case, you can (with some overhead) send/receive binary data as base64 strings... =).
from wsserver.
Thank you for the answer Theldus, I am currently writing a program for an SDR radio and want to send the decoded spectrum data to a web page. First tests with text data and your library look very promising. If I find out how to transfer binary data I will let you know.
from wsserver.
Nice, very nice, I also invite you to do a PR with this feature, if you want.. it would be a great addition have the option to send binary data, ;-).
from wsserver.
Related Issues (20)
- [feature]ws_sendframe_txt_bcast_except(ws_cli_conn_t client, const char *msg);
- [question]What exactly is timeout_ms ? HOT 2
- runtime error for left shifts with fsanitize=address,undefined in sha1.c HOT 3
- ws_getaddress() do not work in onmessage() on localhost HOT 2
- wsserver via ssl in linux environment HOT 1
- Optional forked server application model HOT 2
- <feature> Why not support IPv6 HOT 2
- Only works on a loopback adapter HOT 7
- Why would I want to use civetweb for WebSockets, if this exists? HOT 5
- License Change Consent - wsServer Project HOT 11
- mutiple sockets not working. HOT 16
- Problem with recent change to the calling sequence for the function 'ws_socket() HOT 4
- Server closes the socket after a time. How do I ensure it stays open? HOT 6
- Adding native wss (SSL) support [feature] HOT 4
- How to compile on windows? HOT 1
- [question]Can messages be send to client outside main thread? HOT 3
- [bug] Unable to build with github msys action HOT 2
- [feature] Shutting down websocket HOT 1
- HTTP should be case insentive HOT 3
- [bug]example ping.c hangs when client not respond HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wsserver.