मैं इस समय कुछ मल्टीप्लेयर गेम विचारों को आजमा रहा हूं और एक वेब ब्राउज़र आधारित मल्टीप्लेयर गेम की सेवा के लिए जावा एप्लिकेशन बनाने की कोशिश कर रहा हूं।जावा वेबस्केट होस्ट?
मेरा विकास पर्यावरण मुख्य मशीन पर ग्रहण है, और इस लैपटॉप पर नोटपैड + Google क्रोम है।
मैं क्लाइंट एंड पर जावास्क्रिप्ट का उपयोग कर वेबस्केट बना रहा हूं, और सर्वर अंत में java.net.Socket का उपयोग कर रहा हूं।
मैं दोनों सिरों पर एक कनेक्शन स्वीकार करने में कामयाब रहा हूं, लेकिन क्लाइंट कनेक्शन बंद करने के बिना उनके बीच किसी भी डेटा को भेजने या प्राप्त करने के लिए प्रतीत नहीं होता है (त्रुटि भी नहीं है; बस कुछ चीज़ों पर अजीब लगता है और सॉकेट.क्लोज़ को कॉल करें)।
क्या किसी के पास कोई विचार है?
यहाँ कुछ कोड है:
ग्राहक:
<script type="text/javascript">
var socket;
function init() {
socket = new WebSocket("ws://192.168.0.3:10000");
socket.onopen = function() { alert('OPEN: ' + socket.readyState); }
socket.onmessage = function (msg) { alert('DATA: ' + msg.data); }
socket.onerror = function (msg) { alert('DATA: ' + msg.data); }
socket.onclose = function() { alert('CLOSED: ' + socket.readyState); }
}
function onClick() {
socket.send("YAY!");
}
</script>
सर्वर:
public static void main(String args[])
{
System.out.printLn("Websocket server test");
ServerSocket connectSocket = null;
try
{
Socket clientSocket;
connectSocket = new ServerSocket(10000);
System.out.printLn("Waiting for connection...");
clientSocket = connectSocket.accept();
System.out.printLn("Got one!");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
for(int i=0;i<100;i++) //Shit but easy
{
String data = in.readLine();
System.out.printLn("Got data: " + data);
out.printLn("YAY!");
}
}
catch (IOException e)
{
System.out.printLn("You fail: " + e.getMessage());
}
System.out.printLn("Finished!");
}
क्या कोई कारण है कि आप मौजूदा Websockets सर्वर का उपयोग नहीं करते हैं? –
तैयार किए गए वेबसाइकिल सर्वरों की सूची के लिए यह प्रश्न देखें: http://stackoverflow.com/questions/4278456/websockets-production-ready-server –