Last active
November 30, 2020 23:10
-
-
Save ahmeti/7953725012afcd821c17e383efac32d5 to your computer and use it in GitHub Desktop.
This file contains 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
const fs = require('fs'); | |
const https = require('https'); | |
const WebSocket = require('ws'); | |
const server = https.createServer({ | |
cert: fs.readFileSync('/home/user/certificates/test.com.crt'), // Domain SSL Certificate | |
key: fs.readFileSync('/home/user/certificates/test.com.key'), // Domain SSL Private Key | |
}); | |
const wss = new WebSocket.Server({ server }); | |
const port = 9443; // Burada tanımladığımız port numarasını Nginx sunucumuzda da aynı şekilde tanımlayacağız. | |
wss.on('connection', function connection(ws) { | |
ws.on('message', function incoming(message) { | |
console.log('Alınan Mesaj: %s', message); | |
}); | |
// Bağlanan kullanıcıya ilk mesajımızı gönderiyoruz... | |
let data = JSON.stringify({message: 'Hoşgeldiniz...'}); | |
ws.send(data); | |
}); | |
// Test için her 5 saniyede aktif kullanıcılara mesaj gönder... | |
setInterval(function () { | |
sendMessageActiveUsers('Test'); | |
}, 5000); | |
function sendMessageActiveUsers(message) | |
{ | |
let data = JSON.stringify({message: message}); | |
wss.clients.forEach(function each(client) { | |
if (client.readyState === WebSocket.OPEN) { | |
client.send(data); | |
} | |
}); | |
} | |
server.listen(port); |
This file contains 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
server { | |
listen 443; | |
root /home/user/test.com; | |
index index.php; | |
server_name test.com; | |
ssl on; | |
ssl_certificate /home/user/certificates/test.com.crt; | |
ssl_certificate_key /home/user/certificates/test.com.key; | |
location / { | |
try_files $uri $uri/ /index.php?$query_string; | |
} | |
location /websocket/ { | |
# Aşağıdaki port numarası node js sunucumuzun çalıştığı port numarası olmalıdır! | |
proxy_pass https://localhost:9443; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection "upgrade"; | |
proxy_set_header Host $host; | |
} | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/run/php7.2-fpm-test.sock; | |
} | |
} |
This file contains 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
let socket = new WebSocket("wss://test.com/websocket/"); | |
socket.onopen = function() | |
{ | |
console.log("Socket bağlandı..."); | |
}; | |
socket.onclose = function() | |
{ | |
console.log("Socket bağlantısı kesildi."); | |
}; | |
socket.onmessage = function(event) | |
{ | |
try { | |
let data = JSON.parse(event.data); | |
console.log('Socket mesajı alındı.'); | |
console.log(data); | |
} catch (e) { | |
console.log('Socket mesajı JSON formatında değil'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment