1 Star 0 Fork 0

tengqingya/tiny-game-client

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
tengqingya 提交于 2018-06-06 10:36 . first commit
var ProtoBuf = require("protobufjs");
var root = ProtoBuf.loadSync("./proto/p.proto");
var textMessage = root.lookupType("tutorial.Person");
var payload = {id: 13, email: 'aaa' };
var errMsg = textMessage.verify(payload);
if (errMsg) throw Error(errMsg);
var message = textMessage.create(payload); // or use .fromObject if conversion is necessary
console.log(message)
var buffer = textMessage.encode(message).finish();
var WebSocket = require('ws');
const client = new WebSocket('ws://localhost:10101');
client.binaryType = "arraybuffer";
client.onopen = function () {
console.log(buffer);
client.send(buffer);
}
client.onmessage = function(evt) {
var buf = new Uint8Array(evt.data);
console.log(textMessage.decode(buf))
// const data = root.deserializeBinary(e.data);
// console.log(data)
// var reader = new FileReader();
// reader.readAsArrayBuffer(evt.data);
// reader.onload = function (e) {
// var buf = new Uint8Array(reader.result);
// console.log('<span style="color: blue;">RESPONSE: ' + WSMessage.decode(buf).content + '</span>');
// }
}
client.onclose=function(evt) {
console.log("DISCONNECTED++++++++++++");
}
client.onerror =function(evt) {
console.log("error+++++++++");
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tengqingya/tiny-game-client.git
git@gitee.com:tengqingya/tiny-game-client.git
tengqingya
tiny-game-client
tiny-game-client
master

搜索帮助