Fixed bug with null messages
This commit is contained in:
parent
b71c4c7e4a
commit
fae65b77d7
7 changed files with 108 additions and 100 deletions
|
@ -0,0 +1,5 @@
|
|||
.msg-box{
|
||||
flex: 1 1 auto;
|
||||
overflow-y: auto;
|
||||
height: 32em;
|
||||
}
|
|
@ -31,22 +31,19 @@ function submitForm() {
|
|||
var fetch_messages = new XMLHttpRequest();
|
||||
var url = "/peers/" + window.location.pathname + ".txt";
|
||||
var last_text = "";
|
||||
|
||||
setInterval(function () {
|
||||
fetch_messages.onreadystatechange = function () {
|
||||
if (this.readyState == 4 && this.status == 200) loadMessages(this.responseText.split("\n"));
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
loadMessages(this.responseText.split("\n"));
|
||||
}
|
||||
//$(".msg-box").animate({scrollTop: document.getElementById("msgs").offsetHeight }, "slow");
|
||||
last_text = this.responseText;
|
||||
};
|
||||
fetch_messages.open("GET", url, true);
|
||||
fetch_messages.send();
|
||||
}, 1000);
|
||||
|
||||
function scrollSmoothToBottom (id) {
|
||||
var div = document.getElementById(id);
|
||||
$('#' + id).animate({
|
||||
scrollTop: div.scrollHeight - div.clientHeight
|
||||
}, 500);
|
||||
}
|
||||
|
||||
$(function () {
|
||||
$("#message").keypress(function (e) {
|
||||
var code = (e.keyCode ? e.keyCode : e.which);
|
||||
|
@ -59,7 +56,6 @@ $(function () {
|
|||
});
|
||||
|
||||
|
||||
|
||||
function loadMessages(arr) {
|
||||
var out = "";
|
||||
var i;
|
||||
|
@ -72,8 +68,8 @@ function loadMessages(arr) {
|
|||
out += '<div class="card"><div class="card-body"><h6 class="text-muted card-subtitle mb-2" style="text-align:right;">You<br></h6><p class="card-text" style="text-align:right;">' + joypixels.toImage((decodeURIComponent(msg.replaceAll('+', ' ')))) + '</p></div></div>';
|
||||
}
|
||||
}
|
||||
|
||||
out += "<div id='endl'></div>";
|
||||
var elem = document.getElementById("msgs");
|
||||
elem.innerHTML = out;
|
||||
document.getElementById('end').scrollIntoView();
|
||||
document.getElementById('endl').scrollIntoView();
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/emoji-toolkit@5.0.5/extras/css/joypixels.min.css"/>
|
||||
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="assets/css/styles.min.css">
|
||||
<link rel="stylesheet" href="assets/css/styles.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
|
11
main.c
11
main.c
|
@ -13,24 +13,22 @@
|
|||
|
||||
SOCKET listening;
|
||||
SOCKET web_sock;
|
||||
Map config;
|
||||
PeerList peerList1;
|
||||
|
||||
void closeSocks(void){
|
||||
logger_log("Closing socket...");
|
||||
closesocket(listening);
|
||||
closesocket(web_sock);
|
||||
if(peerList1.size >0)free(peerList1.array);
|
||||
if(config.size > 0) free(config.pairs);
|
||||
}
|
||||
void signalClose(int n){
|
||||
closeSocks();
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
PeerList peerList1;
|
||||
|
||||
atexit(closeSocks);
|
||||
signal(SIGTERM,signalClose);
|
||||
config = config_load();
|
||||
Map config = config_load();
|
||||
|
||||
Node_data mynode = construct_Mynodedata(config);
|
||||
logger_log("Initialising core...");
|
||||
|
@ -83,6 +81,7 @@ int main(void) {
|
|||
|
||||
serverThread(listening,&master,webIo,&peerList1,mynode);
|
||||
|
||||
|
||||
free(peerList1.array);
|
||||
free(config.pairs);
|
||||
return 0;
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
//
|
||||
|
||||
#include "server.h"
|
||||
|
||||
void getSeed(char *output) {
|
||||
FILE *seed_file;
|
||||
seed_file = fopen("seed.txt", "r");
|
||||
|
@ -20,6 +21,7 @@ void getSeed(char*output){
|
|||
}
|
||||
fclose(seed_file);
|
||||
}
|
||||
|
||||
Node_data construct_Mynodedata(Config cfg) {
|
||||
Node_data result;
|
||||
getSeed(result.id);
|
||||
|
@ -75,7 +77,8 @@ void serverThread(SOCKET listening, fd_set *master, WebIO webIo, PeerList* list,
|
|||
if (strlen(buf) == 0)
|
||||
continue;
|
||||
Map m = getPacketData(buf);
|
||||
|
||||
if (map_getValue(m, "message") != NULL) {
|
||||
printf("%s\n", buf);
|
||||
char file[64];
|
||||
int k = peer_getPeer(*list, sock);
|
||||
sprintf(file, "%speers/", webIo.folder);
|
||||
|
@ -88,6 +91,7 @@ void serverThread(SOCKET listening, fd_set *master, WebIO webIo, PeerList* list,
|
|||
logger_log("Message received from %s", list->array[k].peerData.id);
|
||||
FILE *fp;
|
||||
fp = fopen(file, "a");
|
||||
|
||||
fprintf(fp, "%s\n", map_getValue(m, "message"));
|
||||
fclose(fp);
|
||||
free(m.pairs);
|
||||
|
@ -96,3 +100,4 @@ void serverThread(SOCKET listening, fd_set *master, WebIO webIo, PeerList* list,
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -206,7 +206,9 @@ static int webio_handlePOSTrequest(SOCKET client, WebIO wio, Map post) {
|
|||
shutdown(client, SD_BOTH);
|
||||
|
||||
if (map_isFound(post, "id") && map_isFound(post, "message") &&
|
||||
strcmp(map_getValue(post, "message"), "%0D%0A") != 0) {
|
||||
(strcmp(map_getValue(post, "message"), "%0D%0A") != 0 &&
|
||||
strcmp(map_getValue(post, "message"), "(null)") != 0)) {
|
||||
|
||||
char file[64];
|
||||
char folder[72];
|
||||
sprintf(folder, "%s/peers/", wio.folder);
|
||||
|
@ -359,7 +361,7 @@ static void webio_getPeerPage(WebIO wio, char *id, char *outputBuffer) {
|
|||
char *nickname = (online) ? wio.list->array[peer_ID_getPeer(*wio.list, id)].peerData.nick : "";
|
||||
sprintf(content, "%s\n"
|
||||
"<h1>%s%s %s</h1>\n"
|
||||
"<div id=\"msgs\" style=\"margin-bottom:5em\"></div>\n"
|
||||
"<div id=\"msgs\" class=\"msg-box\" style=\"margin-bottom:5em;\"></div>\n"
|
||||
"<div id=\"end\"></div>\n"
|
||||
" <form name=\"sendmsg\" class=\"form-inline\" style=\"margin: 7px;padding: 7px;position: fixed;bottom: 0;width: 100%%;\">"
|
||||
"<textarea name=\"message\" id=\"message\" class=\"form-control\" style=\"width: 90%%;display: block;\" %s></textarea>"
|
||||
|
|
Loading…
Reference in a new issue