【发布时间】:2016-04-21 17:45:45
【问题描述】:
我有一个 laravel 项目,它使用 Node Laravel 和 Redis 进行实时聊天。我遵循了许多教程,但是我迷失了如何将聊天中的数据保存到我的消息数据库(MySql)。我看过这个教程,您可以在其中使用 Redis 使用 laracast 广播事件,还有这个使用 MongoDB https://www.youtube.com/watch?v=c01OHDUpDMU.I 的 youtube 视频还没有找到任何关于套接字保存方法的明确答案。我需要在套接字之外发送 ajax 请求吗??
这里我尝试做一个测试 HttpRequest,也许有办法通过 post 请求将数据发送到我的 Laravel 路由?
谁能提供建议?
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');
});
});
【问题讨论】:
-
抱歉,人们必须付费才能访问您提供的链接。您是否正在尝试将消息保存到 Laravel?
-
啊废话我忘了。是的,我希望能够在发送消息时将消息保存到我的数据库中。我正在考虑提出一个单独的 ajax 请求,但我认为应该有更好的方法来做到这一点。