简体   繁体   English

使用 node.js 和 socket.io 进行 Csharp

[英]Csharp with node.js and socket.io

I wanted to send data from csharp to a webpage live without having to reload or anything annoying like that so I decided to use Node.JS and socket.io我想将数据从 csharp 实时发送到网页,而不必重新加载或任何类似的事情,所以我决定使用 Node.JS 和 socket.io

I am totally new to socket.io and node.js so correct me if this isnt how it works...我对 socket.io 和 node.js 完全陌生,所以如果这不是它的工作原理,请纠正我......

1) I sent data from my Csharp application using the code below. 1) 我使用下面的代码从我的 Csharp 应用程序发送数据。

using Quobject.SocketIoClientDotNet.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Plus.HabboHotel.Roleplay.Extra.SocketIO
{
    public static class SocketIOManager
    {
        public static void SendMessage(string message)
        {
            var socket = IO.Socket("http://127.0.0.1:400");

            socket.On(Quobject.SocketIoClientDotNet.Client.Socket.EVENT_CONNECT, () =>
            {
                socket.Emit("emu_msg", @"Hello from PlusEMU");
                socket.Disconnect();
            });
        }
    }
}

2) The Node.JS app.js picks it up 2) Node.JS app.js 捡起来

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){
  console.log('a user connected');
});

io.on('emu_msg', function(socket){
  console.log('emu message received?');
});

http.listen(400, function(){
  console.log('listening on *:400');
});

Now on the node.js console I should be getting "emu message received" should I not?现在在 node.js 控制台上,我应该不会收到“收到 emu 消息”吗? I am not receiving that我没有收到

Does anybody know why?有人知道为什么吗?

I do receive the 'a user connected' when visiting the webpage that uses the code below访问使用以下代码的网页时,我确实收到了“已连接用户”

<script>
    var socket = io.connect('http://127.0.0.1:400');
    socket.on('emu_msg', function(data) {
        alert(data);
    });
    </script>

But when sending a message with csharp I don't get anything, not even the user connect message, am I missing something here?但是当使用 csharp 发送消息时,我什么也没收到,甚至没有用户连接消息,我在这里遗漏了什么吗? And I don't get the alert on the webpage when sending with csharp either.使用 csharp 发送时,我也没有在网页上收到警报。

on the client you should be listening for the message on the socket.在客户端上,您应该监听套接字上的消息。

io.on('connection', function(socket){
  console.log('a user connected');
  socket.on('emu_msg', function(msg){
    console.log('emu message received?');
  });
});

see: here for more information请参阅: 此处了解更多信息

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

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