woikerman能使用socket.io吗?

ycsi

有例子吗?

6747 7 0
7个回答

ycsi

服务端代码:

use Workerman\Worker;
require_once '../Workerman/Autoloader.php';

$worker = new Worker('websocket://0.0.0.0:8484');
$ws_worker->count = 4;

$worker->onConnect = function($connection)
{
    echo "new connection from ip " . $connection->getRemoteIp() . "\n";

};

$worker->onMessage = function($connection, $data)
{
    var_dump($data);
    $connection->send('receive success');
};

$worker->onError = function($connection, $code, $msg)
{
    echo "error $code $msg\n";
};

$worker->onBufferFull = function($connection)
{
    echo "bufferFull and do not send again\n";
};
$worker->onBufferDrain = function($connection)
{
    echo "buffer drain and continue send\n";
};

$worker->onClose = function($connection)
{
    echo "connection closed\n";
};

$worker->onWorkerStop = function($worker)
{
    echo "Worker stopping...\n";
};

$worker->onWorkerStart = function($worker)
{
    echo "Worker starting...\n";
};

客户端代码:用的是socket.io

private Socket mSocket;
    {
        try {
            mSocket = IO.socket("websocket://192.168.0.11:8484");
        } catch (URISyntaxException e) {}
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity1);

    mSocket.connect();
    mSocket.emit("hello", "hello world");
    mSocket.emit("emit something");
    mSocket.send("send something");
    mSocket.send("hello","thanks");

}

为什么会出现刚连接就断开的情况呢,信息也没有收到

new connection from ip 192.168.0.3
connection closed
new connection from ip 192.168.0.3
connection closed
new connection from ip 192.168.0.3
connection closed
new connection from ip 192.168.0.3
connection closed
new connection from ip 192.168.0.3
connection closed
new connection from ip 192.168.0.3
  • 暂无评论
walkor 打赏

任何客户端都可以与workerman通讯,只要在workerman实现其协议即可。

socket.io也是一样,有自己的应用层协议,在workerman实现这个协议就能直接使用了

  • 暂无评论
ycsi
$ php start.php status
Workerman status
Workerman not run
PHP Warning:  readfile(/var/folders/f3/wm99g0j500n3ytswgpk0f4j00000gn/T/workerman.status): failed to open stream: No such file or directory in /Workerman/Worker.php on line 520

Warning: readfile(/var/folders/f3/wm99g0j500n3ytswgpk0f4j00000gn/T/workerman.status): failed to open stream: No such file or directory in /Workerman/Worker.php on line 520

我两边都实现协议了
服务端:$worker = new Worker('websocket://0.0.0.0:8484');
客户端:mSocket = IO.socket("websocket://192.168.0.11:8484");
可是会出现马上断开的情况。。
上面是运行php start.php status 返回的信息。/Workerman/Worker.php 这个路径明明就存在怎么会说不存在呢?

  • 暂无评论
walkor 打赏

php -v

php -m
贴下

  • 暂无评论
ycsi

php -v
PHP 5.5.27 (cli) (built: Jul 14 2015 17:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

php -m

bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
ldap
libxml
mbstring
mhash
mysql
mysqli
mysqlnd
odbc
openssl
pcntl
pcre
PDO
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib

  • 暂无评论
walkor 打赏

这个最好抓包看下,是谁关闭的连接,这样才好查问题
抓包参见手册 http://doc.workerman.net/debug/tcpdump.html

  • 暂无评论
walkor 打赏

https://github.com/walkor/phpsocket.io
基于workerman实现的Socket.IO 可以直接使用

  • 暂无评论
年代过于久远,无法发表回答
×
🔝