简体   繁体   English

ThreeJS,Websockets和NodeJS客户端/服务器实验

[英]ThreeJS, Websockets, and NodeJS Client/Server Experiment

I was toying with socket.io, ThreeJS, Javascript, and NodeJS to create a simple client/server using ThreeJS's graphics. 我在玩socket.io,ThreeJS,Javascript和NodeJS,以使用ThreeJS的图形创建一个简单的客户端/服务器。 I wasn't sure if all of these frameworks would even work together, but I decided to give it a shot since I've seen similar examples online before even though I cannot find a simple one to dissect or experiment with. 我不确定所有这些框架是否都可以一起工作,但是我决定尝试一下,因为尽管我找不到一个简单的例子来进行剖析或试验,但在网上也曾看到过类似的例子。 It's mainly to experiment with, but I also wanted to make a small little concept-game as proof of what I've learned so far. 主要是要进行试验,但是我也想做一个小的概念游戏,以证明到目前为止我学到的东西。

I posted my code here: https://gist.github.com/netsider/63c414d83bd806b4e7eb 我在这里发布了我的代码: https : //gist.github.com/netsider/63c414d83bd806b4e7eb

Sorry if it's a little untidy, but I did my best to make it as readable as possible. 抱歉,如果有点不整洁,但我已尽力使它尽可能易读。

Basically, right now the server-side NodeJS script seems to run fine (Run with "node server-alpha.js"), and the client script (client-alpha.html, which you can just open in a browser) connects to the server, and displays a list of users (who are also connected). 基本上,现在服务器端的NodeJS脚本似乎运行良好(使用“ node server-alpha.js”运行),而客户端脚本(client-alpha.html,您可以在浏览器中打开它)连接到服务器,并显示用户列表(也已连接)。 However, my intention was for each user to be able to move his/her own cube around, and right now each cube only gets added to the screen (rather than being added, subtracted, and then added again - to give the illusion of movement). 但是,我的目的是让每个用户都可以移动自己的多维数据集,而现在每个多维数据集仅被添加到屏幕上(而不是被添加,减去然后再添加-给出移动的幻觉) )。 If you run both pieces of code and connected one or two users and move the arrow keys a few times for each, you'll see what I'm talking about. 如果您同时运行这两个代码并连接了一个或两个用户,并且每个用户移动了几次箭头键,您将看到我在说什么。

Can anybody help me with this? 有人可以帮我吗? I tried several different ways to remove the cube (and remembered to call render(), after each)... but everything I tried didn't seem to work. 我尝试了几种不同的方法来删除多维数据集(并且记得每次都调用render())……但是我尝试的一切似乎都没有效果。 It always resulted in the cubes just being added to the screen, and never subtracted. 总是导致多维数据集仅被添加到屏幕,而从未被减去。

I added comments in the code to make things a little easier, as I know this is quite a bit of code to go through (if it's not your own, anyway). 我在代码中添加了注释,使事情变得容易一些,因为我知道这需要大量代码(如果不是您自己的话)。

Thanks, any help would be greatly appreciated... as I'm really stuck trying to make the cubes just move. 谢谢,任何帮助将不胜感激...因为我真的很难尝试使立方体移动。

Also, I'm having trouble adding the Fly-Controls (FlyControls.js - it's commented out ATM), so if someone could tell me where I went wrong I'd appreciate that a lot also. 另外,我在添加Fly-Controls时遇到了麻烦(FlyControls.js-在ATM上已注释掉),因此,如果有人可以告诉我我哪里出了问题,我也将不胜感激。

Ok so you don't want to keep remaking the cubes, all you need to do is change the position. 好的,所以您不想继续重建多维数据集,只需要做的就是更改位置。

Also in game development, it is almost a requirement to use object oriented design, a good way to go about this would be to make a player object, so.. 同样在游戏开发中,几乎是使用面向对象的设计的要求,实现此目标的一种好方法是使玩家成为对象。

 CPlayerList = new Array(); // an array of player objects function ClientPlayer() { this.Cube; this.Name = "unnamed"; this.Id = 0; this.Create = function(name,pos,id) { this.Name = name; this.Id = id; var cubeGeometry = new THREE.BoxGeometry(10, 10, 10); var cubeMaterial = new THREE.MeshLambertMaterial({color: 'red', transparent:false, opacity:1.0}); this.Cube = new THREE.Mesh(cubeGeometry, cubeMaterial); this.Cube.position.x = pos.x; this.Cube.position.y = pos.y; this.Cube.position.z = 20; // don't know why this is 20, remember y axis is up & down in opengl/threejs scene.add(this.Cube); } this.Move = function(vector) { this.Cube.position.set(this.Cube.position.x + vector.x, this.Cube.position.y + vector.y, 20); } } 

So on the server you need a ServerPlayer object which holds similar data, and assign ids on the server before sending them to the clients. 因此,在服务器上,您需要一个ServerPlayer对象,该对象保存相似的数据,并在将ID发送给客户端之前在服务器上分配ID。 So when you send it to the client you want to make a new ClientPlayer, call player.Create() and then push it to the CPlayerList, like so: 因此,当您将其发送到客户端时,您想要创建一个新的ClientPlayer,请调用player.Create(),然后将其推送到CPlayerList,如下所示:

 function newCPlayer(data) { var newPly = new ClientPlayer(); newPly.Create(data.name,data.pos,data.id); CPlayerList.push(newPly); } 

Then when you call your movePlayer() function, you can simply loop through your players array 然后,当您调用movePlayer()函数时,您可以简单地遍历玩家数组

 function movePlayer(keyStroke, clientID) { if (keyStroke == 39) { CPlayerList.forEach(function(player,i,a) { if(player.Id === clientID) { player.Move(new THREE.Vector3(1,0,0)); } } } } 

This is just the client code, but this should help you get started, let me know if there's anything you're unclear on. 这只是客户端代码,但这应该可以帮助您入门,如果您不清楚什么,请告诉我。

Also here's an example of a game using a similar design: http://82.199.155.77:3000/ (ctrl+shift+j in chrome to view client sources) and server code: http://pastebin.com/PRPaimG9 另外,这里还有一个使用类似设计的游戏示例:http: http://82.199.155.77:3000/ : http://82.199.155.77:3000/ (Chrome中的ctrl + shift + j以查看客户端资源)和服务器代码: http://pastebin.com/PRPaimG9 : http://pastebin.com/PRPaimG9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM