简体   繁体   English

使用 php 的客户端到客户端套接字

[英]Client to client socket with php

How to make php client to client, like the chat way?如何让 php 客户端到客户端,比如聊天方式? One client connects and sends something to the other client and only he recieve not all clients.一个客户端连接并向另一个客户端发送某些内容,但只有他接收到并非所有客户端。

Your Question?你的问题?

If I understand correctly you want one-to-one(private) messaging.如果我理解正确,您需要一对一(私人)消息传递。

Socket Programming using PHP使用 PHP 进行套接字编程

You need to learn Socket programming with PHP .您需要使用 PHP 学习 Socket 编程 You could start by studying this tutorial .您可以从学习本教程开始。 This has scaling problems written all over it, because PHP does not have non blocking IO, proper thread model.这到处都是缩放问题,因为 PHP 没有非阻塞 IO,正确的线程 model。 I advise you to just use it for fun little projects.我建议你只将它用于有趣的小项目。

Non blocking IO using PHP非阻塞 IO 使用 PHP

You could try and use PHP-MIO .您可以尝试使用PHP-MIO I have not yet tried this, but I guess it might scale.我还没有尝试过,但我想它可能会扩展。 But then again from Apache(PHP) side you will have the same problems.但是从 Apache(PHP) 方面来看,你也会遇到同样的问题。 But when using this from both sides it could work...但是当从双方使用它时,它可以工作......

long-polling(blocking IO) using PHP使用 PHP 进行长轮询(阻塞 IO)

PS: got bored so I have not completely tested this;) PS:无聊所以我没有完全测试过这个;)

Download下载

Below I present two solutions(prototypes) which do NOT scale.下面我介绍了两个无法扩展的解决方案(原型)。 One solution uses Redis pubsub .一种解决方案使用 Redis pubsub For this you need to install(compile) redis .为此,您需要安装(编译) redis For this you a POSIX OS is desired, although some people have ported it to Windows .为此,您需要一个 POSIX 操作系统,尽管有些人已将其移植到 Windows You can also use the free redistogo.com instance.您还可以使用免费的redistogo.com实例。 The Redis solution is the prefered solution. Redis 解决方案是首选解决方案。 I have put everything in an archive which you can download from here .我已将所有内容放在一个存档中,您可以从这里下载。

I also give a solution which uses named pipes.我还给出了一个使用命名管道的解决方案。 This solution does not require you to use Redis, but instead this approach needs access to the file system.此解决方案不需要您使用 Redis,但此方法需要访问文件系统。 I believe that this approach should also work on Windows(You have to change the filename to windows-style).我相信这种方法也应该适用于 Windows(您必须将文件名更改为 windows 样式)。 I would like for somebody to try this out:).我想让有人试试这个:)。 I can not test this anymore, because I have completely switched to POSIX OS(Ubuntu) a long time ago.我不能再测试这个了,因为很久以前我已经完全切换到了 POSIX OS(Ubuntu)。

Requirements要求

At least PHP 5.3 and preferable a POSIX OS, redis .至少 PHP 5.3,最好是 POSIX OS, redis

How to use如何使用

To use both solutions you need to open two browsers(Browser A / B ).要使用这两种解决方案,您需要打开两个浏览器(浏览器A / B )。 I assume you are using localhost for development and that you can access files from http://localhost/6646733 .我假设您正在使用localhost进行开发,并且您可以从http://localhost/6646733访问文件。

  1. point browser A to http://localhost/6646733/redis?me=somebodyelse&to=alfred you should replace redis with pipe when trying out named pipes.将浏览器A指向http://localhost/6646733/redis?me=somebodyelse&to=alfred alfred 您应该在尝试命名管道时将redis替换为pipe
  2. Point browser B to http://localhost/6646733/redis?me=alfred&to=somebodyelse将浏览器B指向http://localhost/6646733/redis?me=alfred&to=somebodyelse
  3. In browser A type a message into the textarea, which will be sent to browser B .在浏览器A中,在 textarea 中输入一条消息,该消息将发送到浏览器B
  4. In browser B read the message just sent from browser A在浏览器B中读取刚刚从浏览器A发送的消息

Solutions not using PHP不使用 PHP 的解决方案

The solutions below scale.规模以下的解决方案。

Pusher(Hosted) Pusher(托管)

With for example the hosted solution Pusher you can do chat/messaging without the scaling nightmare.例如,使用托管解决方案Pusher ,您可以在没有扩展噩梦的情况下进行聊天/消息传递。 Pusher even is generous to provide a free plan. Pusher甚至慷慨地提供免费计划。 But be aware that the cheap plans do NOT offer SSL so the messages can be intercepted.但请注意,便宜的计划不提供 SSL,因此可以拦截消息。 You should never sent private information over the wire, when not using SSL.在不使用 SSL 时,您永远不应该通过网络发送私人信息。 Users/developers have provided a nice little library to use Pusher from PHP .用户/开发人员提供了一个不错的小库来使用来自 PHP 的 Pusher The problem with this approach is that you are not in control, but pusher is, but then again you don't have to worry about any details.这种方法的问题在于您无法控制,但推动者是,但是您不必担心任何细节。

Socket.io(open-source) Socket.io(开源)

I really like socket.io , but there are off course a lot of other solutions like for example tornado .我真的很喜欢socket.io ,但当然还有很多其他的解决方案,例如龙卷风 You could use Redis to efficiently communicate between PHP and the other solution(socket.io).您可以使用 Redis 在 PHP 和其他解决方案(socket.io)之间进行有效通信。

I don't fully understand what you are trying to do, but you can use some kind of database and have it store messages that is sent to each user, and then have your client page refresh the chat part with a AJAX kind of query to update the chat.我不完全理解您要做什么,但是您可以使用某种数据库并让它存储发送给每个用户的消息,然后让您的客户端页面使用 AJAX 类型的查询刷新聊天部分更新聊天。 It will then behave simular to the new Facebook chat, where all messages are stored even the ones sent in normal chat and mail.然后,它的行为类似于新的 Facebook 聊天,其中存储所有消息,即使是在正常聊天和邮件中发送的消息。 So clients can mail and chat each other at all times, when they are online it will show in their chat and when thy are offline it will show up in their inbox.因此,客户可以随时互相发送邮件和聊天,当他们在线时,它会显示在他们的聊天中,当你离线时,它会显示在他们的收件箱中。 But this might not be what you are trying to do.但这可能不是你想要做的。

For implementing best Chat application, use jabber server and write clients using js/flex为了实现最好的聊天应用程序,使用 jabber 服务器并使用 js/flex 编写客户端

http://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocolhttp://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocol

If it's not like chat but you want to send messages over without a server, you need both clients be server as well.如果它不像聊天但你想在没有服务器的情况下发送消息,你需要两个客户端也是服务器。 A server will listen on a port for incomming connections.服务器将在端口上侦听传入连接。 Write a daemon that spawns a new thread each time a client connects.编写一个守护程序,每次客户端连接时都会产生一个新线程。 Within this thread you handle the messaging.在此线程中,您处理消息传递。

Client A opens a connection to the server (Client B) and they can talk to each other.客户端 A 打开与服务器(客户端 B)的连接,它们可以相互通信。 Or you let Client A become server and let Client B connect.或者您让客户端 A 成为服务器并让客户端 B 连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM