我有一个laravel项目,它使用Node laravel和Redis进行实时聊天。我已经关注了很多教程,但我不知道如何将聊天中的数据保存到我的消息数据库(MySql)中。我看到了这个教程,你可以使用Redis用laracast广播事件,还有这个使用MongoDB的youtube视频https://www.youtube.com/watch?v=c01OHDUpDMU.I对于socket的任何保存方法,都没有找到明确的答案。我需要在套接字之外发送ajax请求吗??
在这里,我试图制作一个测试HttpRequest,也许有办法通过post请求将数据发送到我的Laravel Route?
有人能提供建议吗?
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000, function(){
console.log('listening on *:3000');
});
console.log('chat has booted...');
io.on('connection', function(socket){
socket.on('join', function (data) {
socket.userId = data.user_id;
socket.userName = data.user_name;
socket.class_group = data.class_group;
console.log('User ' + socket.userName + ' is now available');
});
//notify connected users availability
socket.on('notify', function (notice) {
console.log( notice + ' from' + ' user ' + socket.userName );
io.emit('receive note.' + notice[0], 'hello');
})
socket.on('subscribe', function (room) {
console.log('joining room', room);
socket.join(room);
})
socket.on('send', function(data){
console.log(socket.userName + ': ' + data.message + ' to room' + '[' + data.room + ']');
io.to(data.room).emit('message', data);
var xhr = new XMLHttpRequest();
xhr.open('GET', "http://google.com", true); //Here I tried to make a test HttpRequest, maybe there is someway to send the data over a post request to my Laravel Route?
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
据我所知,您发布的脚本是将在服务器端执行的Node.js脚本。
为了保存到MongoDB,您可以使用MongoDB驱动程序:https://github.com/mongodb/node-mongodb-native