🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## server::run ### 接口说明 >服务器运行必须接口 > +++ put:run <<< success 无返回值 +++ ### 代码演示 ~~~ #import "socket" var socket_accept(var socketIndex) { println("检测到有一个连接:"..socketIndex); } var socket_data(var socketIndex,var buffer) { var s = buffer.tostring(); println(s); if(s == "e") { srv.closesocket(socketIndex); } var writer = new Buffer(); writer.writeString(buffer.tostring()); srv.send(socketIndex, writer); } var socket_close(var socketIndex,var err,var errstr) { println("检测到有一个关闭:{socketIndex},err:{err},errstr:{errstr}"); } var socket_parser(var headerbuffer) { var packagesize = headerbuffer.readInt32(); return packagesize; // 根据包头协议包大小 } println("start server"); var srv = new socket::server().setoption(new ServerOption(){ ip = "0.0.0.0", accept = socket_accept, data = socket_data, close = socket_close }).listen(4060); //使用带协议头方式 // var srv = new socket::server().setOption(new ServerOption(){ // ip = "0.0.0.0", // headersize = 4,//字节 // accept = socket_accept, // data = socket_data, // close = socket_close, // parser = socket_parser // }).listen(4060); println("start over"); while (true) srv.run(); ~~~