- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
几天后,我阅读了有关如何在本地计算机上设置 laravel-echo 的信息。我设法能够使用 redis 和 socket.io 运行 laravel-echo-server。
问题是事件被触发了,但是客户端没有任何反应。
我知道事件已触发,因为它已登录到我的 horizon.log 文件中。
Horizon started successfully.
[2018-08-29 18:35:30][31] Processing: App\Events\NewModel
[2018-08-29 18:35:31][31] Processed: App\Events\NewModel
然后,在我的 echo.log 文件中,我可以看到用户已连接到 channel 并且 echo 触发了事件。
L A R A V E L E C H O S E R V E R
version 1.3.9
⚠ Starting server in DEV mode...
✔ Running at localhost on port 60010
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
[6:35:15 PM] - QW3VITRzWPH4FvxPAAAB joined channel: channel-name
Channel: presence-channel-name
Event: App\Events\NewModel
它在端口 60010 上运行,因为当我尝试在端口 6001 上运行时,客户端无法连接。
我在 Windows 10 机器上使用 virtualbox/homestead。
有端口转发到我的 Homestead.yaml
ports:
- send: 60010
to: 6001
所以,我不明白为什么客户端不记录事件。这就是为什么我现在的代码
echo .js/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allows your team to easily build robust real-time web applications.
*/
import Echo from "laravel-echo"
window.io = require('socket.io-client');
// Have this in case you stop running your laravel echo server
if (typeof io !== 'undefined') {
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':60010',
});
console.log('Connected to socket.io');
} else {
console.log('Not connected to socket.io');
}
window.Echo.channel('channel-name')
.listen('NewModel', (e) => {
console.log('1:');
console.log(e);
})
.listen('App\Events\NewModel', (e) => {
console.log('2:');
console.log(e);
})
.listen('App.Events.NewModel', (e) => {
console.log('3:');
console.log(e);
});
laravel-echo-server.json
{
"authHost": "https://app.torque.homestead",
"authEndpoint": "/broadcasting/auth",
"clients": [{
"appId": "appId",
"key": "key"
}],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "127.0.0.1"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "",
"port": "60010",
"protocol": "https",
"socketio": {},
"sslCertPath": "app.torque.homestead.crt",
"sslKeyPath": "app.torque.homestead.key",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
channel .php
<?php
/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('channel-name', function () {
return true;
});
如果您想查看其他文件,请告诉我。其实我不知道需要什么来解决这个问题。
我认为问题可能出在转发端口上......但是就像我说的那样,使用 6001 是行不通的。
我尝试了很多东西,比如更改端口(可能不是正确的组合,比如 60010 上的客户端和 6001 上的服务器????)
我也多次将该行更改为 echo.js
host: window.location.hostname + ':60010',
有一些我正在使用的版本:
vagrant@homestead:~$ node -v
v10.8.0
vagrant@homestead:~$ npm -v
6.4.0
vagrant@homestead:~$ php -v
PHP 7.2.9-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:54) ( NTS )
包.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.18",
"babel-preset-react": "^6.24.1",
"bootstrap": "^4.1.3",
"cross-env": "^5.2.0",
"jquery": "^3.2",
"laravel-mix": "^2.1.14",
"lodash": "^4.17.4",
"popper.js": "^1.14.4",
"vue": "^2.5.17"
},
"dependencies": {
"@fortawesome/fontawesome": "^1.1.8",
"@fortawesome/fontawesome-free-brands": "^5.0.13",
"@fortawesome/fontawesome-free-regular": "^5.0.13",
"@fortawesome/fontawesome-free-solid": "^5.0.13",
"@fortawesome/fontawesome-free-webfonts": "^1.0.9",
"@fortawesome/vue-fontawesome": "0.0.22",
"ajv": "^6.5.3",
"bootstrap-confirmation2": "^4.0.1",
"datatables.net": "^1.10.19",
"datatables.net-bs4": "^1.10.19",
"datatables.net-buttons": "^1.5.3",
"datatables.net-buttons-bs4": "^1.5.3",
"express": "^4.16.3",
"ioredis": "^4.0.0",
"laravel-echo": "^1.4.0",
"laravel-echo-server": "^1.3.9",
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1",
"vee-validate": "^2.0.9",
"vue-inject": "^2.1.1",
"yarn": "^1.9.4"
}
}
Composer .json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.1.3",
"aloha/twilio": "^4.0",
"eyewitness/eye": "dev-beta",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "5.7.*",
"laravel/horizon": "^1.3",
"laravel/passport": "^6.0",
"laravel/tinker": "^1.0",
"predis/predis": "^1.1"
},
"require-dev": {
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
在mcklayin推荐我之后,我找了redis数据。所以我运行这个:
vagrant@homestead:/var/lib$ redis-cli --scan
presence-channel-name:members
horizon:1
horizon:34
horizon:supervisors
horizon:measured_jobs
horizon:9
horizon:monitor:time-to-clear
horizon:job:App\Jobs\ProcessModels
horizon:35
horizon:queue:default
horizon:job_id
horizon:job:App\Events\NewModel
horizon:masters
horizon:last_snapshot_at
horizon:33
horizon:master:homestead-xVse
horizon:failed_jobs
horizon:32
horizon:supervisor:homestead-xVse:supervisor-1
horizon:recent_jobs
horizon:measured_queues
有 App\Events\NewModel.php 文件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewModel implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $data;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
public function broadcastAs()
{
return 'NewModel';
}
}
最佳答案
尝试添加到 App\Events\NewModel:
public function broadcastAs()
{
return 'NewModel';
}
并在客户端事件名称前添加点符号:
window.Echo.channel('channel-name')
.listen('.NewModel', (e) => {
console.log('1:');
console.log(e);
})
关于php - Laravel 5.7 - 广播 - socket.io - redis : Nothing happen on client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084681/
我们有一些第三方代码,它们执行以下操作 列表项 创建用户交易,例如 txn = (UserTransaction)ctx.lookup( "UserTransaction" ); txn.begi
表达式 "a".(strlen('ab')-strlen('a')) 按预期计算为 a1。 但是如果我不小心省略了括号, "a".strlen('ab')-strlen('a') 计算结果为-1。这里
我读到了有关 Java 类型删除的内容 on Oracle's website . 什么时候发生类型删除?在编译时还是运行时?类什么时候加载?类何时实例化? 很多网站(包括上面提到的官方教程)都说
我读到了有关 Java 类型删除的内容 on Oracle's website . 什么时候发生类型删除?在编译时还是运行时?类什么时候加载?类何时实例化? 很多网站(包括上面提到的官方教程)都说
我试图对 Haskell 中类型族“发生”的类型级计算何时(以及多少次)形成一种直觉。对于一个具体的例子,考虑这个类型类来索引到 n-ary product使用类型级自然: {-# LANGUAGE
我正在阅读 Java Concurrency in Practice书。 在阅读关于 JMM 的章节时,它说: The JMM defines a partial ordering called ha
我读到了 Java 的类型删除 on Oracle's website . 类型删除何时发生?在编译时还是运行时?什么时候加载类?什么时候实例化类? 很多网站(包括上面提到的官方教程)都说类型删除发生
我一周前通过互联网向 Apple 申请加入 Iphone 开发计划,并向他们发送了我的 99.00 美元。 我的印象是,总的来说,响应速度很快。然而,除了一封确认我购买的电子邮件之外,我什么也没看到。
我读到了 Java 的类型删除 on Oracle's website . 什么时候发生类型删除? 在编译时还是运行时?什么时候加载类?什么时候实例化类? 很多网站(包括上面提到的官方教程)都说类型删
我读到了 Java 的类型删除 on Oracle's website . 类型删除何时发生?在编译时还是运行时?什么时候加载类?什么时候实例化类? 很多网站(包括上面提到的官方教程)都说类型删除发生
有一个快速同步问题,这是我的: a) Class1 的并发 HashMap 定义如下: ConcurrentMap map = new ConcurrentHashMap(); b) Class2
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在研究 Java 并发性并找到了以下资源以供引用, http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/package-su
我根据Douglas Crockford在他的书“Javascript: the good parts”中提出的布局创建了一个对象构造函数。 此构造函数在添加各种成员和方法后返回一个 that 对象。
我正在编写一个单元测试(在 Mocha 中使用 Chai)来查看某个对象 o 是否是 instanceof ClassY。单元测试成功,但我可以让它失败,具体取决于我如何创建对象文字,尽管生成的对象文
假设我有一个包装 HashMap 的类,如下所示: public final class MyClass{ private final Map map; //Called by T
我想弄清楚 happens-before 属性的确切含义。 我看到 happens-before 属性的解释说,如果全局变量(不是易变的或包含在同步块(synchronized block)中)的更新
虽然在 SO 和其他地方有很多关于 happens-before 关系的帖子,但我很难找到我的问题的明确答案。 考虑两个 Java 线程: 最初,flag == false 和 data == 0 T
http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32 #include struct X { X(const char *) { std:
我读到了 Java 的类型删除 on Oracle's website . 类型删除何时发生?在编译时还是运行时?什么时候加载类?什么时候实例化类? 很多网站(包括上面提到的官方教程)都说类型删除发生
我是一名优秀的程序员,十分优秀!