Last active
May 5, 2025 08:19
-
-
Save sunmeat/196f29044aac1951e0270a38b9e6fca7 to your computer and use it in GitHub Desktop.
render.com example client side javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://sunmeat.atwebpages.com/js/sharp.html (client side): | |
<!DOCTYPE html> | |
<html lang=""> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Отправка числа на сервер</title> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
background-color: #121212; | |
color: #ffffff; | |
padding: 2rem; | |
margin: 0; | |
} | |
.container { | |
max-width: 600px; | |
margin: 0 auto; | |
background-color: #1e1e1e; | |
padding: 20px; | |
border-radius: 10px; | |
} | |
input[type="text"] { | |
padding: 8px; | |
border-radius: 5px; | |
border: none; | |
background-color: #2a2a2a; | |
color: #ffffff; | |
width: 100%; | |
box-sizing: border-box; | |
margin-bottom: 10px; | |
} | |
button { | |
padding: 0.5rem 1rem; | |
margin: 5px; | |
border-radius: 5px; | |
border: none; | |
background-color: #1db954; | |
color: #ffffff; | |
cursor: pointer; | |
font-size: 16px; | |
} | |
button:hover { | |
background-color: #1ed760; | |
} | |
button:disabled { | |
background-color: #555; | |
cursor: not-allowed; | |
} | |
.result-box { | |
margin: 20px 0; | |
padding: 10px; | |
border: 1px solid #444; | |
border-radius: 5px; | |
text-align: left; | |
max-height: 300px; | |
overflow-y: auto; | |
} | |
.success { | |
color: #00FF00; | |
} | |
.error { | |
color: #FF5555; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h2>Отправка числа на сервер</h2> | |
<input type="text" id="numberInput" placeholder="Введите число или 'exit'" value="5" /> | |
<button onclick="sendNumber()" id="sendButton">Отправить</button> | |
<button onclick="stopSending()" id="stopButton" disabled>Остановить</button> | |
<div id="result" class="result-box"></div> | |
</div> | |
<script> | |
const serverUrl = 'https://netserver-dkf4.onrender.com/send'; | |
const resultDiv = document.querySelector('#result'); | |
const numberInput = document.querySelector('#numberInput'); | |
const sendButton = document.querySelector('#sendButton'); | |
const stopButton = document.querySelector('#stopButton'); | |
let isSending = false; | |
async function sendNumber() { | |
if (isSending) return; // Prevent multiple sends | |
const input = numberInput.value.trim(); | |
if (!input) { | |
appendResult('Пожалуйста, введите число или "exit".', 'error'); | |
return; | |
} | |
if (input.toLowerCase() === 'exit') { | |
stopSending(); | |
return; | |
} | |
isSending = true; | |
sendButton.disabled = true; | |
stopButton.disabled = false; | |
appendResult('Отправка...', 'success'); | |
try { | |
const response = await fetch(serverUrl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'text/plain', | |
'Accept': 'text/plain' | |
}, | |
body: input | |
}); | |
if (!response.ok) { | |
const errorText = await response.text(); | |
throw new Error(`Ошибка ${response.status}: ${errorText}`); | |
} | |
const result = await response.text(); | |
document.title = `Сервер ответил: ${result}`; | |
appendResult(`Сервер ответил: ${result}`, 'success'); | |
} catch (error) { | |
appendResult(`Ошибка отправки: ${error.message}`, 'error'); | |
} finally { | |
isSending = false; | |
sendButton.disabled = false; | |
numberInput.value = ''; | |
numberInput.focus(); | |
} | |
} | |
function stopSending() { | |
isSending = false; | |
sendButton.disabled = false; | |
stopButton.disabled = true; | |
appendResult('Отправка остановлена.', 'success'); | |
document.title = 'Отправка числа на сервер'; | |
} | |
function appendResult(message, type) { | |
const messageDiv = document.createElement('div'); | |
messageDiv.className = type; | |
messageDiv.textContent = message; | |
resultDiv.appendChild(messageDiv); | |
resultDiv.scrollTop = resultDiv.scrollHeight; | |
} | |
numberInput.addEventListener('keypress', (e) => { | |
if (e.key === 'Enter') { | |
sendNumber(); | |
} | |
}); | |
</script> | |
</body> | |
</html> | |
===================================================================================================== | |
https://github.com/sunmeat/NetServer/blob/master/Program.cs (server side): | |
using System; | |
using System.Net; | |
using System.Text; | |
using System.IO; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static async Task Main() | |
{ | |
string port = Environment.GetEnvironmentVariable("PORT") ?? "5000"; | |
string url = $"http://+:{port}/send/"; | |
HttpListener listener = new HttpListener(); | |
listener.Prefixes.Add(url); | |
listener.Start(); | |
Console.WriteLine($"Сервер запущен на {url}"); | |
while (true) | |
{ | |
var context = await listener.GetContextAsync(); | |
_ = Task.Run(() => ProcessRequest(context)); | |
} | |
} | |
static async Task ProcessRequest(HttpListenerContext context) | |
{ | |
HttpListenerRequest request = context.Request; | |
HttpListenerResponse response = context.Response; | |
// add CORS headers to all responses (for JavaScript example) | |
response.AddHeader("Access-Control-Allow-Origin", "*"); | |
response.AddHeader("Access-Control-Allow-Methods", "POST, OPTIONS"); | |
response.AddHeader("Access-Control-Allow-Headers", "Content-Type"); | |
// handle preflight OPTIONS request (for JavaScript example) | |
if (request.HttpMethod == "OPTIONS") | |
{ | |
response.StatusCode = 200; | |
response.Close(); | |
return; | |
} | |
if (request.HttpMethod != "POST") | |
{ | |
response.StatusCode = 405; | |
await response.OutputStream.WriteAsync(Encoding.UTF8.GetBytes("Только POST-запросы")); | |
response.Close(); | |
return; | |
} | |
using var reader = new StreamReader(request.InputStream, Encoding.UTF8); | |
string numberStr = await reader.ReadToEndAsync(); | |
if (int.TryParse(numberStr, out int number)) | |
{ | |
int result = number + 1; | |
byte[] buffer = Encoding.UTF8.GetBytes(result.ToString()); | |
response.ContentType = "text/plain"; | |
response.ContentLength64 = buffer.Length; | |
await response.OutputStream.WriteAsync(buffer, 0, buffer.Length); | |
Console.WriteLine($"Получено: {number}, отправлено: {result}"); | |
} | |
else | |
{ | |
response.StatusCode = 400; | |
await response.OutputStream.WriteAsync(Encoding.UTF8.GetBytes("Ошибка: ожидалось число.")); | |
} | |
response.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment