Download the PHP package naivic/oswserver without Composer
On this page you can find all versions of the php package naivic/oswserver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download naivic/oswserver
More information about naivic/oswserver
Files in naivic/oswserver
Package oswserver
Short Description Naivic OpenSwoole-based Universal gRPC+HTTP+Websocket Server
License MIT
Informations about the package oswserver
Naivic\OswServer
Example: Two HTML pages from two servers, talks each other
Installation and preparation
- Go example/host
- Do 'composer update'
- Go example (cd ..)
- Do 'docker-compose up -d --build'
- Open three tabs in browser - one for Alice and two for Bob:
- One for Alice - go http://127.0.0.1:11080/
- One for Bob - go http://127.0.0.1:12080/
- And one more for Bob - go http://127.0.0.1:12080/ again
Test and use
On the Alice's tab, type 'Alice' in the field 'Your name', type 'hello' in textarea below, and press [enter].
You will see your message in log on Alice's tab as 'Message from me', and on both Bob's tabs as 'Message from Alice'.
Also you will see server's message "The message has been sent to 2 client connections" on the Alice's tab.
Switch to one of Bob's tab, type 'Bob' in the field 'Your name', type 'hello' in textarea below, and press [enter].
You will see your message in log on both Bob's tabs, and in log on tab of Alice.
You can also test connection/disconnection situations by stopping/restarting containers and closing/reopening browser tabs.
You can monitor the message flow through the osw1 and osw2 container logs.
Basic use case of OswServer
0. See the code example
Refer to the code in /example/host at all stages, it really helps.
1. Design a gRPC service
We need to describe our gRPC service and messages in .proto format and compile the .proto into PHP classes (for a more detailed explanation, see the gRPC tutorials)
Let's describe a simple symmetric host-to-host protocol. Two servers exchange messages, each message has a sender's nickname and text. That's it.
2. Add code to gRPC service class
We need to write our code in gRPC service class.
Just send a message from gRPC peer to our websocket client.
Note the function $serv->sendMsgToClient(), we'll discuss it later.
3. Create main server class
We need to define our main server class, which will do all the work. This class should be a child of \Naivic\OswClass.
4. Define onStart() method to get IP address of peer
We can define an onStart method if we want to do some additional initialization. Here we get the peer IP address from the environment variable.
5. Add code to route messages from peer to client
We need a method to deliver a message from a peer to our client - sendMsgToClient(), mentioned in our function OpenSwoole\GRPC\HostService::Message().
6. Add code to handle HTTP and Websocket requests
We need to define our processing methods:
- processRequestHttp() - for HTTP requests processing;
- processRequestWs() - for WebSocket requests processing.
For example, make loading of the root html page. Refer to the file /example/host/root.html to see client part of code.
Add a method to handle a message from a client. We share messages and delivery statuses across all client connections on a same server.
7. (Optional) Add code to custom gRPC processing and manage Websocket client connections
If we want make some custom processing of gRPC requests - we can add the optional processRequestGrpc() method.
And we can add some code to manage client connections.
8. Start main server
We start our server in usual \OpenSwoole\GRPC\Server manner.
It is not necessary to open different ports for gRPC and WebSocket/HTTP connections. But, if you want, you can easily do it:
9. Add spices to taste
You can easily extend this code to achieve your own goals. Good Luck!