<?php
require_once "vendor/autoload.php";
use dollarphp\autofunction;
use dollarphp\errorhandle;
new autofunction('function');
new errorhandle();
use Workerman\Worker;
use GatewayWorker\Gateway as gate;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Register;
use GatewayWorker\Lib\Gateway;
$gateway = new gate("websocket://0.0.0.0:2345");
$gateway->startPort = 30000;
$gateway->count = 4;
$gateway->name = 'gateway';
$gateway->registerAddress = '127.0.0.1:1234';
$business = new BusinessWorker();
$business->count = 4;
$business->registerAddress = '127.0.0.1:1234';
$register = new Register('text://0.0.0.0:1234');
$register->name = 'register';
class Events{
public static function onConnect($client_id){
return GateWay::sendToCurrentClient('welcome');
}
public static function onMessage($client_id, $message){
$arr = array(
'remote_ip'=>$_SERVER['REMOTE_ADDR'],
'remote_port'=>$_SERVER['REMOTE_PORT'],
'gate_ip'=>$_SERVER['GATEWAY_ADDR'],
'gate_port'=>$_SERVER['GATEWAY_PORT'],
'client_id'=>$_SERVER['GATEWAY_CLIENT_ID'],
);
var_dump($arr);
return GateWay::sendToCurrentClient($message);
}
public static function onClose($client_id){
GateWay::sendToAll("client[$client_id] logout\n");
}
}
Worker::runAll();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text">
<br>
<input type="button" value="提交">
<script>
var ws = new WebSocket('ws://192.168.8.81:2345')
ws.onopen = function(data) {
console.log('連接開始\n')
}
ws.onmessage = function(data) {
console.log('接收消息:'+data.data)
}
ws.onclose = function(data) {
console.log('關(guān)閉連接\n')
}
ws.onerror = function(data) {
console.log('遇到錯誤\n')
}
var text = document.getElementsByTagName('input')[0]
var button = document.getElementsByTagName('input')[1]
button.onclick = function(){
var val = text.value
ws.send(val)
}
</script>
</body>
</html>
新聞名稱:workerman實(shí)現(xiàn)群聊
鏈接分享:http://aaarwkj.com/article0/pjcioo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站排名、網(wǎng)站設(shè)計、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)