thinkphp6使用workerman websocket连接 (带源码)
一、首先按照thinkphp文档中的进行安装workerman
地址:https://www.kancloud.cn/manual/thinkphp6_0/1147857
- composer require topthink/think-worker
- php think worker
- php think worker:server
- websocket客户端代码:
<!DOCTYPE html>
<html>
<head>
<title>HTML5</title>
<meta charset="utf-8" />
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function() {
var socket;
var readyState = ["connecting", "connected", "closing", "closed"];
/* 打开连接事件 */
$("button:eq(0)").click(function() {
try {
/* 连接 */
socket = new WebSocket("ws://外网地址:2345");
/* 绑定事件 */
socket.onopen = function() {
$("#msg").html("连接成功...");
};
socket.onmessage = function(e) {
$("#msg").html($("#msg").html() + "<br />" + e.data);
};
socket.onclose = function() {