如何在触发套接字断开连接事件时调用laravel事件或函数

时间:2016-12-13 16:10:41

标签: javascript php node.js laravel socket.io

我使用的是Laravel 5.2,我想要的是从套接字事件断开调用自定义函数来更新表playsessions中的记录。

过程

  1. 当用户进入频道部分并通过AJAX发送用户的设备信息时,例如:ip,devide,browser,socket id等,以便在数据库中创建记录

  2. 但如果用户断开连接,我想更新发送套接字ID的记录。

  3. 实际上,我执行了第1步,但是,我不知道如何进行第二步。

    查看Channel.blade.php

    var socket = io.connect('http://localhost:3000');
    
    socket.on('connect', function() {
        var socket_id = socket.io.engine.id;
    
        $.ajax({
            type: 'POST',
            url: "{{ route('play_session.store') }}",
            data: {
                ip: "{{ $play_session_data['ip'] }}",
                device: "{{ $play_session_data['device'] }}",
                platform: "{{ $play_session_data['platform'] }}",
                browser: "{{ $play_session_data['browser'] }}",
                session_id: "{{ $play_session_data['session_id'] }}",
                user_id: "{{ $play_session_data['user_id'] }}",
                channel_id: "{{ $play_session_data['channel_id'] }}",
                socket_id: socket_id
            },
            success: function() {
                console.log('session created');
            }
        });
    });
    

    Socket.js

    var app = require('express')();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);
    var redis = require('redis');
    
    server.listen(3000);
    
    io.on('connection', function (socket) {
        console.log("new client connected " + socket.id);
    
        socket.on('disconnect', function() {
            console.log('client disconnected');
    
            // I want to call here a event of laravel or function sending the socket.id to update a record in database 
        });
    });
    

    或者当用户从laravel服务器中断开连接时,最好的方法是什么。

    更新

    我现在在事件断开时使用AJAX但是没有工作:

    查看Channel.blade.php

    var socket = io.connect('http://localhost:3000');
    
    socket.on('connect', function() {
        var socket_id = socket.io.engine.id;
    
        $.ajax({
            type: 'POST',
            url: "{{ route('play_session.store') }}",
            data: {
                ip: "{{ $play_session_data['ip'] }}",
                device: "{{ $play_session_data['device'] }}",
                platform: "{{ $play_session_data['platform'] }}",
                browser: "{{ $play_session_data['browser'] }}",
                session_id: "{{ $play_session_data['session_id'] }}",
                user_id: "{{ $play_session_data['user_id'] }}",
                channel_id: "{{ $play_session_data['channel_id'] }}",
                socket_id: socket_id
            },
            success: function() {
                console.log('session created');
            }
        });
    
        socket.on('disconnected', function() {
            $.ajax({
                type: 'POST',
                url: "{{ route('play_session.close') }}",
                data: {
                    socket_id: socket_id
                },
                success: function() {
                    console.log('session closed');
                }
            });
        });
    });
    

    感谢。

1 个答案:

答案 0 :(得分:0)

您可以发送包含所需数据的AJAX请求,并从那里触发事件。