@@ -3,14 +3,16 @@ var opn = require("open");
33var express = require ( "express" ) ;
44var app = express ( ) ;
55var server = require ( "http" ) . createServer ( app ) ;
6- var io = require ( "socket.io" ) ( server ) ;
76var mineflayer = require ( "mineflayer" ) ;
87var Chunk = require ( "prismarine-chunk" ) ( version ) ;
98var vec3 = require ( "vec3" ) ;
109var Convert = require ( "ansi-to-html" ) ;
1110var convert = new Convert ( ) ;
1211var helmet = require ( "helmet" ) ;
1312var compression = require ( "compression" ) ;
13+ const WebSocket = require ( "ws" ) ;
14+ const qs = require ( "qs" ) ;
15+ const { encode, decode } = require ( "@msgpack/msgpack" ) ;
1416var port = process . env . PORT || 8080 ;
1517
1618app . use (
@@ -19,6 +21,12 @@ app.use(
1921 } )
2022) ;
2123app . use ( compression ( ) ) ;
24+
25+ const wss = new WebSocket . Server ( {
26+ server,
27+ clientTracking : false ,
28+ } ) ;
29+
2230var mode = process . argv [ 2 ] ;
2331if ( mode === "production" ) {
2432 app . use ( express . static ( `${ __dirname } /client/dist` ) ) ;
@@ -35,9 +43,16 @@ server.listen(port, function () {
3543 opn ( `http://localhost:${ port } ` ) ;
3644 return console . log ( `Server is running on \x1b[34m*:${ port } \x1b[0m` ) ;
3745} ) ;
46+
3847var botByNick = { } ;
39- io . sockets . on ( "connection" , function ( socket ) {
40- var query = socket . handshake . query ;
48+
49+ wss . on ( "connection" , ( socket , req ) => {
50+ const emit = ( type , ...data ) => {
51+ socket . send ( encode ( [ type , ...data ] ) ) ;
52+ } ;
53+
54+ const query = qs . parse ( req . url . substr ( 1 ) , { ignoreQueryPrefix : true } ) ;
55+
4156 console . log ( `[\x1b[32m+\x1b[0m] ${ query . nick } ` ) ;
4257 var heldItem = null ;
4358 var bot = mineflayer . createBot ( {
@@ -51,29 +66,29 @@ io.sockets.on("connection", function (socket) {
5166 bot . _client . on ( "map_chunk" , function ( packet ) {
5267 var cell = new Chunk ( ) ;
5368 cell . load ( packet . chunkData , packet . bitMap , true , true ) ;
54- socket . emit ( "mapChunk" , cell . sections , packet . x , packet . z ) ;
69+ emit ( "mapChunk" , cell . sections , packet . x , packet . z ) ;
5570 } ) ;
5671 bot . _client . on ( "respawn" , function ( packet ) {
57- socket . emit ( "dimension" , packet . dimension . value . effects . value ) ;
72+ emit ( "dimension" , packet . dimension . value . effects . value ) ;
5873 } ) ;
5974 bot . on ( "heldItemChanged" , function ( item ) {
6075 heldItem = item ;
6176 } ) ;
6277 bot . on ( "login" , function ( ) {
63- socket . emit ( "dimension" , bot . game . dimension ) ;
78+ emit ( "dimension" , bot . game . dimension ) ;
6479 } ) ;
6580 bot . on ( "move" , function ( ) {
66- socket . emit ( "move" , bot . entity . position ) ;
81+ emit ( "move" , bot . entity . position ) ;
6782 } ) ;
6883 bot . on ( "health" , function ( ) {
69- socket . emit ( "hp" , bot . health ) ;
70- socket . emit ( "food" , bot . food ) ;
84+ emit ( "hp" , bot . health ) ;
85+ emit ( "food" , bot . food ) ;
7186 } ) ;
7287 bot . on ( "spawn" , function ( ) {
73- socket . emit ( "spawn" , bot . entity . yaw , bot . entity . pitch ) ;
88+ emit ( "spawn" , bot . entity . yaw , bot . entity . pitch ) ;
7489 } ) ;
7590 bot . on ( "kicked" , function ( reason ) {
76- socket . emit ( "kicked" , reason ) ;
91+ emit ( "kicked" , reason ) ;
7792 } ) ;
7893 bot . on ( "message" , function ( msg ) {
7994 let message = msg . toAnsi ( ) ;
@@ -87,34 +102,34 @@ io.sockets.on("connection", function (socket) {
87102 for ( const replacement of replacements )
88103 message = message . replace ( replacement [ 0 ] , replacement [ 1 ] ) ;
89104
90- socket . emit ( "msg" , convert . toHtml ( message ) ) ;
105+ emit ( "msg" , convert . toHtml ( message ) ) ;
91106 } ) ;
92107 bot . on ( "experience" , function ( ) {
93- socket . emit ( "xp" , bot . experience ) ;
108+ emit ( "xp" , bot . experience ) ;
94109 } ) ;
95110 bot . on ( "blockUpdate" , function ( oldb , newb ) {
96- socket . emit ( "blockUpdate" , [
111+ emit ( "blockUpdate" , [
97112 newb . position . x ,
98113 newb . position . y ,
99114 newb . position . z ,
100115 newb . stateId ,
101116 ] ) ;
102117 } ) ;
103118 bot . on ( "diggingCompleted" , function ( block ) {
104- socket . emit ( "diggingCompleted" , block ) ;
119+ emit ( "diggingCompleted" , block ) ;
105120 } ) ;
106121 bot . on ( "diggingAborted" , function ( block ) {
107- socket . emit ( "diggingAborted" , block ) ;
122+ emit ( "diggingAborted" , block ) ;
108123 } ) ;
109124 bot . on ( "game" , function ( ) {
110- socket . emit ( "game" , bot . game ) ;
125+ emit ( "game" , bot . game ) ;
111126 } ) ;
112127 var inv = "" ;
113128 var interval = setInterval ( function ( ) {
114129 var inv_new = JSON . stringify ( bot . inventory . slots ) ;
115130 if ( inv !== inv_new ) {
116131 inv = inv_new ;
117- socket . emit ( "inventory" , bot . inventory . slots ) ;
132+ emit ( "inventory" , bot . inventory . slots ) ;
118133 }
119134 var entities = {
120135 mobs : [ ] ,
@@ -134,17 +149,20 @@ io.sockets.on("connection", function (socket) {
134149 ] ) ;
135150 }
136151 }
137- socket . emit ( "entities" , entities ) ;
152+ emit ( "entities" , entities ) ;
138153 } , 100 ) ;
154+
155+ const handlers = new Map ( ) ;
156+
139157 bot . once ( "spawn" , function ( ) {
140- socket . on ( "fly" , function ( toggle ) {
158+ handlers . set ( "fly" , function ( toggle ) {
141159 if ( toggle ) {
142160 bot . creative . startFlying ( ) ;
143161 } else {
144162 bot . creative . stopFlying ( ) ;
145163 }
146164 } ) ;
147- socket . on ( "blockPlace" , function ( pos , vec ) {
165+ handlers . set ( "blockPlace" , function ( pos , vec ) {
148166 var block = bot . blockAt ( new vec3 ( ...pos ) ) ;
149167 if ( heldItem !== void 0 && heldItem !== null ) {
150168 console . log ( heldItem ) ;
@@ -153,44 +171,44 @@ io.sockets.on("connection", function (socket) {
153171 } ) ;
154172 }
155173 } ) ;
156- socket . on ( "invc" , function ( num ) {
174+ handlers . set ( "invc" , function ( num ) {
157175 var item = bot . inventory . slots [ num + 36 ] ;
158176 if ( item !== null && item !== void 0 ) {
159177 bot . equip ( item , "hand" ) ;
160178 } else if ( heldItem !== void 0 ) {
161179 bot . unequip ( "hand" ) ;
162180 }
163181 } ) ;
164- socket . on ( "move" , function ( state , toggle ) {
182+ handlers . set ( "move" , function ( state , toggle ) {
165183 if ( state === "right" ) {
166184 state = "left" ;
167185 } else if ( state === "left" ) {
168186 state = "right" ;
169187 }
170188 bot . setControlState ( state , toggle ) ;
171189 } ) ;
172- socket . on ( "command" , function ( com ) {
190+ handlers . set ( "command" , function ( com ) {
173191 bot . chat ( com ) ;
174192 } ) ;
175- socket . on ( "rotate" , function ( data ) {
193+ handlers . set ( "rotate" , function ( data ) {
176194 bot . look ( ...data ) ;
177195 } ) ;
178- socket . on ( "disconnect" , function ( ) {
196+ handlers . set ( "disconnect" , function ( ) {
179197 try {
180198 clearInterval ( interval ) ;
181199 console . log ( `[\x1b[31m-\x1b[0m] ${ query . nick } ` ) ;
182200 bot . end ( ) ;
183201 } catch ( error ) { }
184202 } ) ;
185- socket . on ( "dig" , function ( pos ) {
203+ handlers . set ( "dig" , function ( pos ) {
186204 var block = bot . blockAt ( vec3 ( pos [ 0 ] , pos [ 1 ] - 16 , pos [ 2 ] ) ) ;
187205 if ( block !== null ) {
188206 var digTime = bot . digTime ( block ) ;
189207 if ( bot . targetDigBlock !== null ) {
190208 console . log ( "Already digging..." ) ;
191209 bot . stopDigging ( ) ;
192210 }
193- socket . emit ( "digTime" , digTime , block ) ;
211+ emit ( "digTime" , digTime , block ) ;
194212 console . log ( "Start" ) ;
195213 bot . dig ( block , false , function ( xd ) {
196214 if ( xd === void 0 ) {
@@ -201,8 +219,20 @@ io.sockets.on("connection", function (socket) {
201219 } ) ;
202220 }
203221 } ) ;
204- socket . on ( "stopDigging" , function ( ) {
222+ handlers . set ( "stopDigging" , function ( ) {
205223 bot . stopDigging ( ) ;
206224 } ) ;
225+
226+ socket . on ( "message" , ( message ) => {
227+ try {
228+ const [ type , ...data ] = decode ( message ) ;
229+
230+ const handler = handlers . get ( type ) ;
231+
232+ handler ( ...data ) ;
233+ } catch ( err ) {
234+ console . log ( err ) ;
235+ }
236+ } ) ;
207237 } ) ;
208238} ) ;
0 commit comments