[英]Communicating with Node.js server code on Compute Engine using socket.io and C# client
I currently have a Compute Engine and App Engine working and a C# client application. 我目前有一个计算引擎和App Engine工作和一个C#客户端应用程序。 My goal is to connect my C# client app with google cloud and work real-time with requests and websocket. 我的目标是将我的C#客户端应用程序与谷歌云连接,并实时处理请求和websocket。
For that purpose, after reading lots of documentations and guides, I decided to go with this logic: 为此,在阅读了大量文档和指南之后,我决定采用这种逻辑:
And for that, I tried using socket.io
without any luck. 为此,我尝试使用socket.io
没有任何运气。
Issue here is my mydomain.appspot.com
url is working fine, at least it shows a text without any change. 这里的问题是我的mydomain.appspot.com
网址工作正常,至少它显示的文字没有任何变化。
I get Listening port xxxx
on my terminal, yet no changes occur. 我在终端上获得了Listening port xxxx
,但没有发生任何变化。
I feel like I'm listening to something but not actually connected to it so code under .connect
doesn't run. 我觉得我正在听某些东西,但实际上没有连接它,所以.connect
下的代码不能运行。
I'm getting Port is in use
error (Exit status code 8 error, but I looked it up and it means that) when I listen to whatever port, yet when I show active processes on that port, there are none. 我收到Port is in use
错误(退出状态代码8错误,但我查了一下,这意味着)当我听到任何端口,但当我在该端口上显示活动进程时,没有。
Are there any additional configurations should I make on the Google Cloud part, other than the ones in official Bookshelf tutorial? 除了官方Bookshelf教程中的配置之外,我是否应该在Google Cloud部分上进行任何其他配置?
Server Code 服务器代码
var io = require('socket.io').listen(65080);
io.sockets.on('connection', function (socket) {
var address = socket.handshake.address;
console.log('New connection from ' + address.address + ':' + address.port);
io.sockets.emit('this', { will: 'be received by everyone'});
socket.on('private message', function (msg) {
console.log('New Chat Message ', msg);
io.sockets.emit('txt',msg);
});
socket.on('disconnect', function () {
io.sockets.emit('User Disconnected');
});
socket.on('newuser', function (name) {
console.log(name,' Is Now Connected!');
io.sockets.emit('txnpm start
t',name + ' is now online');
});
socket.on('exit', function (name) {
console.log(name,' Has Been Disconnected!');
io.sockets.emit('txt',name + ' is now offline');
});
});
Client Code 客户代码
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
user = "x";
MyForm = new Form1();
temp = new Form2();
Application.Run(temp);
try
{
socket = new Client(ip.ToString());
socket.On("txt", (data) =>
{
//MessageBox.Show(data.RawMessage);
String msg = data.Json.Args[0].ToString();
Console.Write(msg);
MyForm.update(msg);
//MessageBox.Show(msg, "Received Data");
});
socket.Connect();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(),"Something Went Wrong!!");
Application.Exit();
}
if (socket.ReadyState.ToString() == "Connecting")
{
userset();
Application.Run(MyForm);
}
else
{
MessageBox.Show("Failed To Connect To Server!", "Error!");
Application.Exit();
}
&& &&
using (var ws = new WebSocketSharp.WebSocket("ws://104.197.217.126:65080/"))
{
ws.OnMessage += (sender, e) =>
MessageBox.Show(e.Data);
ws.Connect();
}
EDIT: 编辑:
Also tried a different approach on client side by following the client Android code provided by socket.io, just to make sure if the issue was with my C# client code. 还通过遵循socket.io提供的客户端Android代码在客户端尝试了不同的方法,只是为了确保问题是否与我的C#客户端代码有关。 Still no result, same situation persists. 仍然没有结果,同样的情况仍然存在。
Android Client Code Android客户端代码
Socket mSocket;
Activity activity;
public void connect(Activity a){
activity=a;
try {
mSocket = IO.socket("http://104.197.217.126:65080");
} catch (URISyntaxException e) {}
mSocket.on("new message", onNewMessage);
mSocket.connect();
// mSocket.emit("new message", "sadasdasd");
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONException e) {
return;
}
// add the message to view
Toast.makeText(activity,username+"-"+message,Toast.LENGTH_LONG);
}
});
}
};
In conclusion, these are the steps I took: 总之,这些是我采取的步骤:
My code for client app looks like this: 我的客户端应用程序代码如下所示:
var socket = IO.Socket("http://104.198.214.221:65080"); // this ip has to be your instance's external IP, left to the SSH button on your Compute Engine instances page. socket.On(Socket.EVENT_CONNECT, () => { socket.Emit("private message", "Hello. I'm connected!"); }); socket.On("private message", (data) => { MessageBox.Show(data.ToString()); socket.Disconnect(); });
Bam! 巴姆! As my server code was running on instance computer, I got the "New Connection from: bla:bla" message as soon as the client app connection function called, and then received the "private message" event emit also. 由于我的服务器代码在实例计算机上运行,一旦客户端应用程序连接函数被调用,我就得到了“New Connection from:bla:bla”消息,然后接收到“私有消息”事件也会发出。
It was a rookie mistake on my side. 这是我身边的一个新手错误。 First thing to make sure is that you are using the socket.io client libraries, otherwise it won't work. 首先要确保您使用的是socket.io客户端库,否则它将无法正常工作。 And second, you need to connect to your IP and port using http://, not ws:// or wss://. 其次,您需要使用http://,而不是ws://或wss://连接到您的IP和端口。
Hope this helps someone in future who is struggling with such simple problem. 希望这可以帮助将来遇到这样一个简单问题的人。 Thanks to everyone else who did their best to help despite my lack of information. 感谢所有尽管我缺乏信息而竭尽全力帮助的人。
You say app engine listens for requests? 你说app引擎会监听请求吗? WS requests or HTTP requests? WS请求或HTTP请求?
Some things I've noticed. 我注意到的一些事情。
By default all ws urls should look like this ws://104.197.217.126:65080
or simply 104.197.217.126:65080
unless the client library allows you to specify the port. 默认情况下,除非客户端库允许您指定端口,否则所有ws url应如下ws://104.197.217.126:65080
或简称为104.197.217.126:65080
。 I suggest maybe sticking to the default port:6455
at first. 我建议可能坚持使用默认port:6455
。
Also you don't need to use GAE + CCE to run a ws server. 此外,您不需要使用GAE + CCE来运行ws服务器。 Its far more straight forward just using GCE. 使用GCE更直接。 You could run a single CGE instance just for ws. 你可以只为ws运行一个CGE实例。
But since you mentioned GAE im going to assume you used one of those "nodejs on app engine" tutorials to get started. 但是既然你提到了GAE我会假设你使用其中一个“appj引擎上的节点”教程来开始。 The issue here is that currently this is currently in beta. 这里的问题是目前这个版本目前处于测试阶段。 App engine will create GCE instances to run the code but the code will only be accessible through the .appspot.com domain. 应用程序引擎将创建GCE实例以运行代码,但代码只能通过.appspot.com域访问。 So myapp.appspot.com:65080
might work 所以myapp.appspot.com:65080
可能会有效
I'm still unclear of architecture of your setup and if you need GAE, but other than that, the nodejs code seems fine and should work by itself. 我仍然不清楚你的设置的架构,如果你需要 GAE,但除此之外,nodejs代码似乎很好,应该自己工作。
I suggest using Websocket.org to do some more testing if how you connect to the ws connection first. 如果你首先连接到ws连接,我建议使用Websocket.org做更多的测试。
Let me know what you think. 让我知道你的想法。
I ran some tests: 我跑了一些测试:
It seems as though socket.io was built specifically to run with socket.io client libraries. 似乎socket.io是专门为与socket.io客户端库一起运行而构建的。 So trying to access the ws connection from a standard ws library will result in headache. 因此,尝试从标准ws库访问ws连接将导致头痛。 This is because the actual socket.io Uri will resemble ws://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=1474301877690-0
这是因为实际的socket.io Uri将类似于ws://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=1474301877690-0
I've found a library built for socket.io but haven't tested it. 我找到了一个为socket.io构建的库,但还没有测试过它。
https://github.com/Quobject/SocketIoClientDotNet https://github.com/Quobject/SocketIoClientDotNet
Alternatively however I was able to use standard ws libraries and the websocket node.js library to see something working. 或者,我可以使用标准的ws库和websocket node.js库来查看有效的工作。
try out https://github.com/websockets/ws 试试https://github.com/websockets/ws
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.