PDA

View Full Version : [CLOSED] MessageBus publish method



gets_gui
Apr 24, 2012, 5:04 PM
Hi,

I have a problem for MessageBus publish.

publisher side:

protected void PublishEvent(object sender, DirectEventArgs e)
{
MessageBus.Default.Publish("App.Refresh", "It is an event from server side");
}
subscribe side:

protected void ServerBusEvent(object sender, DirectEventArgs e)
{
...
}

<MessageBusDirectEvents>
<ext:MessageBusDirectEvent Name="App.Refresh" OnEvent="ServerBusEvent">
<ExtraParams>
<ext:Parameter Name="message" Value="data" Mode="Raw" />
</ExtraParams>
</ext:MessageBusDirectEvent>
</MessageBusDirectEvents>

The code works (ServerBusEvent method will get invoked) if the publisher code is in the aspx, it does not work when it is sent from a controller method as following:

public string Upload()
{
...
MessageBus.Default.Publish("App.Refresh", "It is an event from server side");
return ...
}

Please advise.

Vladimir
Apr 24, 2012, 10:09 PM
At this moment, scripts are not registered automatically in the controller action, please do it manually



public ActionResult ClickTest()
{
AjaxResult r = new AjaxResult();


MessageBus.Default.Publish("App.Refresh", "It is an event from server side");
r.Script = MessageBus.Default.GetGeneratedScripts();


return r;
}

gets_gui
Apr 25, 2012, 3:15 PM
I tried the way you suggested, it does not work.
It seems that the
MessageBus.Default.GetGeneratedScripts(); returns an empty string.
BTW, I have the latest code from SVN.

Vladimir
Apr 25, 2012, 3:39 PM
I cannot reproduce it, please provide your test case

gets_gui
Apr 25, 2012, 5:28 PM
Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">


protected void PublishEvent(object sender, DirectEventArgs e)
{
MessageBus.Default.Publish("App.Refresh", "It is an event from server side");
}



protected void ServerBusEvent(object sender, DirectEventArgs e)
{

var Html = e.ExtraParams["message"];

}


</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title id="title2">Jefferies Global Electronic Trading Service - Reconcile Blocks
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="/Content/main.css" />
<script type="text/javascript">
var Upload = function () {
App.formPanel.getForm().url = "/Home/Upload";
App.formPanel.getForm().submit();
};
</script>
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" />

</head>
<body >
<ext:ResourceManager ID="ResourceManager1" runat="server" IDMode="Explicit" />

<ext:FormPanel ID="formPanel" runat="server" Layout="TableLayout" FileUpload="True">
<MessageBusDirectEvents>
<ext:MessageBusDirectEvent Name="App.Refresh" OnEvent="ServerBusEvent">
<ExtraParams>
<ext:Parameter Name="message" Value="data" Mode="Raw" />
</ExtraParams>
</ext:MessageBusDirectEvent>
</MessageBusDirectEvents>
<Items>
<ext:Button ID="Button1" runat="server" Text="Publish from aspx server">
<directevents>
<Click OnEvent="PublishEvent" />
</directevents>
</ext:Button>
<ext:FileUploadField ID="fileUploadField" Name="upload" runat="server" Width="200" Icon="Attach" ButtonText="Upload...">
<listeners>
<Change Handler="Upload()"/>
</listeners>
</ext:FileUploadField>
</Items>
</ext:FormPanel>
</body>
</html>

controller code:

using System.Web.Mvc;

namespace Ext.Net.MVC.Demo.Controllers
{
[HandleError]
public class HomeController : System.Web.Mvc.Controller
{

public ActionResult Index()
{


return this.View();
}



[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload()
{
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.IsUpload = true;
MessageBus.Default.Publish("App.Refresh", "It is an event from controller");
ajaxResult.Script = MessageBus.Default.GetGeneratedScripts();

return ajaxResult;
}

}
}

Daniil
Apr 25, 2012, 9:27 PM
Hi,

FormPanel doesn't execute a script from a response, you should run it manually within a submit callback.

Example

App.formPanel.getForm().submit({
callback : function (result) {
eval(result.script);
}
});

Also please use FormResult instead of AjaxResult.

gets_gui
Apr 26, 2012, 1:19 PM
Daniil, thank you for the reply.
The FormResult does not exist in v2 and I guess we have to stick with AjaxResult.
I believe there maybe some bugs/issues with form.
However, I have the following example which has the same problem (the script is empty) without form.


<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">


protected void PublishEvent(object sender, DirectEventArgs e)
{
MessageBus.Default.Publish("App.Refresh", "It is an event from server side");
}



protected void ServerBusEvent(object sender, DirectEventArgs e)
{

var Html = e.ExtraParams["message"];

}


</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title id="title2">Jefferies Global Electronic Trading Service - Reconcile Blocks
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="/Content/main.css" />
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" />

</head>
<body >
<ext:ResourceManager ID="ResourceManager1" runat="server" IDMode="Explicit" />

<ext:Panel ID="Panel2" runat="server">

<MessageBusDirectEvents>
<ext:MessageBusDirectEvent Name="App.Refresh" OnEvent="ServerBusEvent">
<ExtraParams>
<ext:Parameter Name="message" Value="data" Mode="Raw" />
</ExtraParams>
</ext:MessageBusDirectEvent>
</MessageBusDirectEvents>
<Buttons>
<ext:Button ID="Button1" runat="server" Text="Publish from aspx server">
<directevents>
<%--<Click OnEvent="PublishEvent" /> --%>
<Click Url="/Home/Upload" />
</directevents>
</ext:Button>
</Buttons>
</ext:Panel>
</body>
</html>


using System.Web.Mvc;

namespace Ext.Net.MVC.Demo.Controllers
{
[HandleError]
public class HomeController : System.Web.Mvc.Controller
{

public ActionResult Index()
{


return this.View();
}


public ActionResult Upload()
{
AjaxResult result = new AjaxResult();
result.IsUpload = true;
MessageBus.Default.Publish("App.Refresh", "It is an event from controller");
result.Script = MessageBus.Default.GetGeneratedScripts();
return result;
}

}
}

Daniil
Apr 26, 2012, 2:17 PM
My fault, I meant "FormPanelResult". Generally, any FormPanel actions like submit expects a FormPanelResult response.

Regarding to the problem with

result.Script = MessageBus.Default.GetGeneratedScripts();

It seems to don't work with

result.IsUpload = true;

We are investigating further.

Daniil
Apr 26, 2012, 4:32 PM
After updating from SVN the following example will work as expected.

Example View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:FormPanel runat="server">
<MessageBusListeners>
<ext:MessageBusListener Name="App.Refresh" Handler="console.log(data);" />
</MessageBusListeners>
<Items>
<ext:FileUploadField runat="server" />
</Items>
<Buttons>
<ext:Button runat="server" Text="Publish from aspx server">
<DirectEvents>
<Click Url="/Test/Upload" />
</DirectEvents>
</ext:Button>
</Buttons>
</ext:FormPanel>
</body>
</html>



Example Controller Action

public ActionResult Upload()
{
AjaxResult result = new AjaxResult();
result.IsUpload = true;
MessageBus.Default.Publish("App.Refresh", "It is an event from controller");
return result;
}

gets_gui
Apr 26, 2012, 5:48 PM
Your example works fine and it is very nice that we don't need to register the script in controller.

However, I changed back to the form submit and it doesn't work any more. :(.
I tried to return FormPanelResult instead of AjaxResult, it didn't make any difference.
And I aslo try to add call back in the submit (BTW, there is no result.script any more) and it didn't help.


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net.MVC v2 Example</title>
<script type="text/javascript">
var Upload = function () {
App.FormPanel1.getForm().url = "/Home/Upload";
App.FormPanel1.getForm().submit();
};
</script>
</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:FormPanel ID="FormPanel1" runat="server">
<MessageBusListeners>
<ext:MessageBusListener Name="App.Refresh" Handler="console.log(data);" />
</MessageBusListeners>
<Items>
<ext:FileUploadField ID="FileUploadField1" runat="server" />
</Items>
<Buttons>
<ext:Button ID="Button1" runat="server" Text="Publish from aspx server">
<%--<DirectEvents>
<Click Url="/Home/Upload" />
</DirectEvents>--%>
<Listeners>
<Click Handler="Upload()"/>
</Listeners>
</ext:Button>
</Buttons>
</ext:FormPanel>
</body>
</html>

Vladimir
Apr 26, 2012, 6:37 PM
Hi,

FormPanelResult was not updated to return generated script automatically. Please update from SVN now
Please note that MVC functionality is not updated in Ext.Net v2 yet therefore mvc api is not consistent yet (and buggy), we will improve it in the final release

According you sample, i prepared two samples (one with formpanel.getForm().submit, second with button direct event)

First sample,


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net.MVC v2 Example</title>
<script type="text/javascript">
var Upload = function () {
App.FormPanel1.getForm().url = "/Examples/Upload";
App.FormPanel1.getForm().submit({
success : function (form, action) {
eval(action.result.script);
}
});
};
</script>
</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:FormPanel ID="FormPanel1" runat="server">
<MessageBusListeners>
<ext:MessageBusListener Name="App.Refresh" Handler="alert(data);" />
</MessageBusListeners>
<Items>
<ext:FileUploadField ID="FileUploadField1" runat="server" />
</Items>
<Buttons>
<ext:Button ID="Button1" runat="server" Text="Publish from aspx server">
<Listeners>
<Click Handler="Upload()"/>
</Listeners>
</ext:Button>
</Buttons>
</ext:FormPanel>
</body>
</html>


public ActionResult Upload()
{
FormPanelResult result = new FormPanelResult();
result.IsUpload = true;
MessageBus.Default.Publish("App.Refresh", "It is an event from controller");

return result;
}




Second sample


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:FormPanel ID="FormPanel1" runat="server">
<AutoEl Tag="Form" />
<MessageBusListeners>
<ext:MessageBusListener Name="App.Refresh" Handler="alert(data);" />
</MessageBusListeners>
<Items>
<ext:FileUploadField ID="FileUploadField1" runat="server" />
</Items>
<Buttons>
<ext:Button ID="Button1" runat="server" Text="Publish from aspx server">
<DirectEvents>
<Click Url="/Examples/Upload" IsUpload="true" />
</DirectEvents>
</ext:Button>
</Buttons>
</ext:FormPanel>
</body>
</html>


public ActionResult Upload()
{
AjaxResult result = new AjaxResult();
result.IsUpload = true;
MessageBus.Default.Publish("App.Refresh", "It is an event from controller");

return result;
}

gets_gui
Apr 26, 2012, 7:00 PM
Great, it works!
Thanks a lot!

gets_gui
Apr 26, 2012, 8:08 PM
I still have a question related with the publish.
I have a static thread in the Global.asax.cs, it will listen to a socket and when there is a data in the socket, it will try to broadcast the data.
And I try to use the messagebus publish method as well.

I got an exception in X.cs

return (HttpContext.Current.Items["Ext.Net.Js"] ?? (HttpContext.Current.Items["Ext.Net.Js"] = new Js())) as Js;
And it turns out that HttpContext.Current is null.
Is this a bug or MessageBus does not support this kind of behaviour?

Vladimir
Apr 26, 2012, 8:15 PM
It means that not active request. You cannot do it in thread. You have to use Ext.Net in active request context. Server cannot push request to the client (you can review Commet functionality but it is out of Ext.Net scope), it can answer only

For example, you can save any required info some where (when you received that info using a socket), on client side make periodical requests to server (TaskManager or something else) and check saved info

gets_gui
Apr 26, 2012, 8:22 PM
I was thinking to replace websync (one of the comet servers we are using currently) with MessageBus...
Anyway, thanks for the information.