我在本地计算机上完成了一个成功的实时通知模块.我需要在live server中实现这个模块.实时服务器是https.我不知道我们需要为https更改哪些内容.请检查我的代码并帮助将其集成到https服务器上.谢谢.
ssl文件
SSLCertificateFile /etc/letsencrypt/live/dev.test.de/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/dev.test.de/privkey.pem
socket.js
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var redis = require('redis'); io.on('connection',function(socket){ console.log('a user connected'); var redisClient = redis.createClient(); /* Realtime message subscribe */ redisClient.subscribe('message'); redisClient.on('message',function(channel,message){ /*console.log('new message in queue',channel,message);*/ socket.emit(channel,message); }); /* Realtime inquiry subscribe */ redisClient.subscribe('inquiryCount'); redisClient.on('inquiryCount',message){ /*console.log('new inquiry in queue',message); }); socket.on('disconnect',function(){ redisClient.quit(); console.log('user disconnected'); }); }); http.listen(3000,function(){ console.log('listening on *:3000'); });
app.blade.PHP
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script> <script> var socket = io('{{env('APP_URL')}}:3000'); /* Realtime message notification */ socket.on('message',function(data){ if(data){ /* var res = $.parseJSON(data);*/ $('.messages-menu').empty(); $('.messages-menu').html(data); } }); /* Realtime inquiry notification */ socket.on('inquiryCount',function(data){ if(data){ $('.tasks-menu').empty(); $('.tasks-menu').html(data); } }); </script>
DashboardController.PHP
public function privateMessageAPICount($id) { $message = 'taking count of data'; if(count($message) > 0){ $redis = Redis::connection(); $redis->publish('message',$message); return response()->json(['status' => 'success'],200); } else { return response()->json(['status' => 'No Result'],404); } } public function inquiryAPIUnreadCount($id) { $inquiryCount .= 'taking count of data'; if(count($inquiryCount ) > 0){ $redis = Redis::connection(); $redis->publish('inquiryCount',$inquiryCount); return response()->json(['status' => 'success'],404); } }
当您的apache服务器已经使用https配置时,您只需要对您的ws服务器实施https.由于这是一个节点http服务器,这个修改过的代码应该可以工作(基于这个
post):
原文链接:https://www.f2er.com/php/133138.htmlvar app = require('express')(); var fs = require('fs'); var privateKey = fs.readFileSync('/etc/letsencrypt/live/dev.test.de/privkey.pem','utf8'); var certificate = fs.readFileSync('/etc/letsencrypt/live/dev.test.de/fullchain.pem','utf8'); var credentials = {key: privateKey,cert: certificate}; var https = require('https').createServer(credentials,app); var io = require('socket.io')(https); var redis = require('redis'); io.on('connection',function(){ redisClient.quit(); console.log('user disconnected'); }); }); https.listen(3000,function(){ console.log('listening on *:3000'); });
您的APP_URL必须以https://开头.
我建议使用laravel-echo-server来处理ws-connections,它更简单