我已在此聊天中添加了语音,因此现在它是语音/文本组合聊天。您可以在 github 上找到我的贡献,网址如下:
https://github.com/HowardRichman/simple-text-voice-chat-in-node-and-javascript
以下是在 Centos 6.9 中使用 Node.js 服务器和 jquery javascript 客户端进行安全 Websocket 聊天的工作示例。仅涉及两个文件:(1) server.js 和 (2) client.htm。
以下是我使用以下 Linux 命令行运行的 server.js 代码:node server.js
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const server = new https.createServer({
cert: fs.readFileSync('/var/cpanel/ssl/apache_tls/example.com/combined'),
key: fs.readFileSync('/var/cpanel/ssl/apache_tls/example.com/combined')
});
const wss = new WebSocket.Server({ server });
var msg;
wss.on('connection', function connection(ws)
{
ws.on('message', function incoming(message)
{
msg = message;
console.log('received: %s', msg);
wss.clients.forEach(function (client)
{
if (client.readyState == WebSocket.OPEN)
{
client.send( msg …Run Code Online (Sandbox Code Playgroud)