2017-03-06 67 views
2

我想在web应用程序中创建聊天应用程序,用户可以与不同的站点用户聊天。这将在网上和iOS上提供。使用PHP和Websockets实时聊天应用程序

而不是使用传统的轮询技术(在1秒的时间间隔内发送ajax命中到服务器),我想使用websockets。

通过几个教程,但在他们所有的人都做了PUBLIC GROUP聊天。 (示例网址:https://www.sanwebe.com/2013/05/chat-using-websocket-php-socket

任何人都可以有想法如何开发使用PHP的私人聊天& Websockets。

我有websockets的基本思想,但如何使用它们发布特定频道上的数据?如果我们有40个用户,那么我们需要创建40个不同的频道?

在此先感谢。

+0

[编写一个聊天应用程序的可能的复制](http://stackoverflow.com/questions/3682198/writing-a-chat-application) –

+0

你显示一个例子的链接,然后问任何想法?在SO上,人们在代码问题上的帮助?并说出你的朋友停止upvote你的问题。你的问题是广泛的,而不是代码相关。 – JustOnUnderMillions

+1

@JustOnUnderMillions,那个链接只是websocket的演示教程。除了代码问题之外,我们也可以讨论架构问题。我只想知道如何实现上述方法 –

回答

1

对于私人(房间)聊天系统,你必须开发自己的逻辑。 我会建议您使用以下库:

http://socketo.me/

通过他们的文档在http://socketo.me/docs/开始编码。 如果您遇到问题,请发布您的代码和社区以便帮助

+0

同意,我们必须我们自己的逻辑。但是,你可以请分享链接/教程使用套接字io创建2个通道。逻辑部分对我来说很清楚,我目前陷入困境的是如何创建多个通道,并在客户端部分如何仅订阅特定通道。 –

+0

您可以访问github存储库以供参考,并使用php套接字使用https://github.com/pmill/php-chat完成多房间私人聊天的代码。 –

2

与做单一全球聊天和多个专用频道没有多大区别。首先,你需要设计一个协议。让我们创建一个简单的协议:

// client send to server 
JOIN <channel_id> 
LEAVE <channel_id> 
MSG <channel_id> <message> 

// server send to client 
JOIN <channel_id> <username> 
LEAVE <channel_id> <username> 
MSG <channel_id> <username> <message> 
  • 因此,当用户连接到服务器,就可以随机指定他的用户名。你有一个数组来存储所有连接。
  • 创建通道数组。每个通道在通道内都有一个用户数组。
  • 当客户端发送JOIN <channel_id>到服务器。广播JOIN <channel_id> <username>到该频道的所有连接。
  • 当客户端发送MSG <channel_id> <message>到服务器。广播MSG <channel_id> <username> <message>到该频道的所有连接。
  • 等和....

所以基本上,WebSocket的沟通提供了一个基本的方式,它是高达你创造性地做事情。

+0

不要写你自己的协议,这是重塑WAMP之一。你应该看看http://socketo.me/docs/wamp – Alcalyn

0

这就是我在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

  1. 制作一个自定义的工匠命令使用棘轮

    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(); 
    } 
    

    }

你的控制器应运行某些端口上的WebSockets像下面

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) { 
    } 
} 

广播类应该如下

namespace App\Events; 
use App\Events\Event; 
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 
use Illuminate\Queue\SerializesModels; 

class MessageBroadcast extends Event implements ShouldBroadcast 
{ 
    use SerializesModels; 

    public $message,$datetime,$userid; 

    public function __construct($message,$datetime,$userid) 
    { 
     $this->message = $message; 
     $this->datetime = $datetime; 
     $this->userid = $userid; 


    } 

    public function broadcastOn() 
    { 
     return ['test-channel'.$this->user_id]; 
    } 
} 

JavaScript部分订阅的频道

<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