I'm very new to this so I really don't know what I'm doing, but I've setup a node.js udp server. I want to send a packet to it from the client (being from a website), but I don't know how to do that in javascript/ or if it's even possible. I'm not looking on how to send a packet from a node.js client to server, but rather how to write a website to send a packet to a node.js udp server. Not sure if that made sense, but thanks for the help!
问题:
回答1:
You cannot send UDP datagrams from a webbrowser (i.e. JavaScript). What you can do is contact a webserver (for example via AJAX or websocket) and execute a server-side program (in php or node.js or so) to send the UDP datagram. You can send a UDP datagram with the server-side programming language's native socket module (Python, php, node.js).
However, why are you using UDP in the first place? You'll have to handle retransmissions, reordering and the like, and since webbrowsers can only talk via TCP, you're not getting any of the advantages of UDP.
回答2:
You may be interested in this tutorial:
http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/
It goes through how to create a chat application in node.js in very fine detail, he even shows how to use Socket.io (a browser js library) to utilize web sockets for realtime, low latency responses.
And as Phihag hinted at, UDP isn't good for this sort of application because UDP does not guarantee your packet will get to its destination. You definitely want to use TCP Sockets in this case.
More reading:
- http://en.wikipedia.org/wiki/Transmission_Control_Protocol
- http://en.wikipedia.org/wiki/User_Datagram_Protocol