简体   繁体   中英

How to send text from textbox with SignalR from client to hub

I'm struggling with a beginner problem. After hours asking google I didn't find the right answer (maybe there is no good explanation in German).

I created a hub. On my client I want to send a text which is filled in a textbox to my hub. But it doesn't work. i tried every tutorial from the web. anybody can help me?

here is my code:

Hub Class:

class myhub : Hub
{
    public void sendPatName (string name)
    {
        Clients.All.broadcastMessage(name);
        Console.WriteLine (name);
    }
}

Client Side code

var hubConnection = new HubConnection("http://192.168.188.33:8080");
IHubProxy PatScreenProxy = hubConnection.CreateHubProxy("myhub");

//this doesn't work
// PatScreenProxy.On<string>("boradcastMessage", (param) => this.Invoke((Action)(() => textBox2.AppendText(string.Format("{0}", param)))));

hubConnection.Start().Wait();
string PatName = this.txtbLastname.Text;
PatScreenProxy.Invoke("sendPatName", PatName);

when I try

PatScreenProxy.Invoke("sendPatName", "PatName");

I receive the string PatName at my hub.

How can I send the content of my textbox?

I believe you problem is related to the line:

     PatScreenProxy.Invoke("sendPatName", PatName);

Try to explicit convert it to string:

     PatScreenProxy.Invoke("sendPatName", PatName.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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