cocos creator 3.6.0引入socket.io

1、socket.io官网https://socket.io/
2、客户端使用的是socket.io-client,别弄错了,服务端使用socket.io
3、cocos使用socket的时候,使用package.json配置文件,或者nmp install socket.io-client
需要引入dist下面的,这个才是web版本用到的,不然会报typeError错误
TypeError: Class extends value undefined is not a constructor or null
import {io} from "socket.io-client"; 改为下面的
import {io} from "socket.io-client/dist/socket.io.js";
4、引入后报错没有了,但是TypeScript的提示也没有了,需要自己建立d.ts文件,随便在工作目录下面建立一个SocketExchange.d.ts文件,写入内容
declare module "socket.io-client/dist/socket.io.js" {
export * from "socket.io-client";
}
就可以使用提示了

不得不说,cocoscreator的坑感觉好多

参考:https://discuss.cocos2d-x.org/t/v3-0-and-socket-io-client-npm-module/52910/7

你可能感兴趣的:(cocos creator 3.6.0引入socket.io)