이것은 내가 Laravel에서 한 것입니다. Predis, socket.io, ratchet 및 기타 종속성을 설치해야합니다.https://laracasts.com/discuss/channels/general-discussion/step-by-step-guide-to-installing-socketio-and-broadcasting-events-with-laravel-51
래칫
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Server\IoServer;
class webSockets extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'run:socket {port?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run websockets for specified port';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$port = $this->argument('port');
$server = IoServer::factory(
new ChatController(),$port
$server->run();
}
}
당신의 컨트롤러가 있어야한다을 사용하여 일부 포트에서 WebSocket을 실행 한 사용자의 장인 명령을 확인 확인하시기 바랍니다 같은
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatController implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
}
public function onMessage(ConnectionInterface $from, $msg) {
//FIRE A BROADCAST EVENT HERE
event(new MessageBroadcast(
$message,
$datetime,
$user_id
)
);
}
public function onClose(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
}
}
아래
방송의 클래스
<script src="{ { asset('js/socket.io.js') } }"></script>
<script>
//var socket = io('http://localhost:3000');
var socket = io('http://yourip:5000');
socket.on("test-channel1:App\\Events\\EventName", function(message){
// get user on console
console.log(message);
});
</script>
당신은 배경에서에서 [이 채팅 응용 프로그램 작성의
1. php artisan run:socket <port_no>
2. Node yourjavascript.js
가능한 복제를 다음 명령을 실행해야 채널을 구독 할 수
자바 스크립트 부분 아래 같아야합니다 ] (http://stackoverflow.com/questions/3682198/writing-a-chat-application) –
예제에 대한 링크를 보여주고 어떤 아이디어가 있습니까? 그래서 사람들이 코드 문제에 도움을 줍니까? 그리고 당신의 친구들이 당신의 질문을 upvote하지 말라고 말하십시오. 질문은 넓고 코드와 관련이 없습니다. – JustOnUnderMillions
@JustOnUnderMillions, 그 링크는 websocket에 대한 데모 튜토리얼 일뿐입니다. 코드 문제와 별개로 아키텍처 문제도 논의 할 수 있습니다. 그냥 접근 방법을 알고 싶습니다. 위의 방법을 알고 싶습니다. –