这节具体看看 websocket-networking 是如何启动startup 的
从前面知道,必须有个
server-events 文件夹
const features = [ // quest goals/rewards have to be loaded before areas that have quests which use those goals { path: 'quest-goals/', fn: 'loadQuestGoals' }, { path: 'quest-rewards/', fn: 'loadQuestRewards' }, { path: 'attributes.js', fn: 'loadAttributes' }, // any entity in an area, including the area itself, can have behaviors so load them first { path: 'behaviors/', fn: 'loadBehaviors' }, { path: 'channels.js', fn: 'loadChannels' }, { path: 'commands/', fn: 'loadCommands' }, { path: 'effects/', fn: 'loadEffects' }, { path: 'input-events/', fn: 'loadInputEvents' }, { path: 'server-events/', fn: 'loadServerEvents' }, { path: 'player-events.js', fn: 'loadPlayerEvents' }, { path: 'skills/', fn: 'loadSkills' }, ];
下面仅有一个文件wss.js ,内容如下
'use strict'; // import 3rd party websocket library const WebSocket = require('ws'); const { Logger } = require('ranvier'); // import our adapter const WebsocketStream = require('../lib/WebsocketStream'); module.exports = { listeners: { startup: state => function (commander) { // create a new websocket server using the port command line argument const wss = new WebSocket.Server({ port: 4001 }); // This creates a super basic "echo" websocket server wss.on('connection', function connection(ws) { // create our adapter const stream = new WebsocketStream(); // and attach the raw websocket stream.attach(ws); // Register all of the input events (login, etc.) state.InputEventManager.attach(stream); stream.write("Connecting...\n"); Logger.log("User connected via websocket..."); // @see: bundles/ranvier-events/events/login.js stream.emit('intro', stream); }); Logger.log(`Websocket server started on port: ${wss.options.port}...`); }, shutdown: state => function () { // no need to do anything special in shutdown }, } };
再看 adpator 的作用 ,在 lib 目录下 WebsocketStream.js
'use strict'; const { TransportStream } = require('ranvier'); /** * Essentially we want to look at the methods of WebSocket and match them to the appropriate methods on TransportStream */ class WebsocketStream extends TransportStream { attach(socket) { super.attach(socket); // websocket uses 'message' instead of the 'data' event net.Socket uses socket.on('message', message => { this.emit('data', message); }); } get writable() { return this.socket.readyState === 1; } write(message) { if (!this.writable) { return; } // this.socket will be set when we do `ourWebsocketStream.attach(websocket)` this.socket.send(JSON.stringify({ type: 'message', message, })); } pause() { this.socket.pause(); } resume() { this.socket.resume(); } end() { // 1000 = normal close, no error this.socket.close(1000); } executeSendData(group, data) { if (!this.writable) { return; } this.socket.send(JSON.stringify({ type: 'data', group, data })); } } module.exports = WebsocketStream;
多了一层的作用没看出来,可以用多个网络底层?
socket.on('message', message => { this.emit('data', message); });
将原始websocket 的 message 转化为 data 事件
在telnet bunndle 下的node_modlues 下,找到telnetsocket 的代码
attach(connection) { this.socket = connection; let inputbuf = new Buffer(this.maxInputLength); let inputlen = 0; /** * @event TelnetSocket#error * @param {Error} err */ connection.on('error', err => this.emit('error', err)); this.socket.write("\r\n"); connection.on('data', (databuf) => { databuf.copy(inputbuf, inputlen); inputlen += databuf.length;
看到了, on data ,哈哈,多了一个适配器,统一了。
标签:websocket,socket,startup,wss,源码,path,message,data,fn From: https://www.cnblogs.com/cslie/p/17090997.html