嘿希望有人能帮我一把.
我正在尝试使用Scala Actors和Netty.io库来获取异步http请求.(是的,我知道Scala演员被弃用,但这对我来说是一次学习练习)
我编写了一个actor HttpRequestActor,它接受一个案例类RequestPage(uri:URI)形式的消息.
当它收到消息时它会创建必要的Netty对象需要发出http请求,我基于[ HttpSnoopClient]的大部分代码(http://static.netty.io/3.5/xref/org/jboss/netty/ example/http/snoop/HttpSnoopClient.html)示例.
我创建一个客户端并将当前的actor实例传递给我的实现,ChannelPipelineFactory该实现也将actor传递给我的实现SimpleChannelUpstreamHandler,在那里我重写了该messageReceived函数.
actor实例作为监听器传递,我使用DefaultHttpRequest该类创建请求并写入通道以发出请求.
使用ChannelFuture从写入通道返回的对象对actor对象进行阻塞调用.当messageRecieved我的处理程序类的函数被调用时,我将netty http请求的响应解析为字符串,使用响应的内容向actor发送消息并关闭通道.
在未来完成之后,我的代码尝试向收到的http内容响应的主叫actor发送回复.
代码工作,我能够得到答复,将其发送到我的actor实例,打印出内容并向正在使用的actor实例释放资源发送消息.
问题是当我测试它时,对actor的原始调用没有得到回复而且线程只是保持打开状态.
我HttpRequestActor班上的代码
import scala.actors.Actor
import java.net.{InetSocketAddress,URI}
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.bootstrap.ClientBootstrap
import org.jboss.netty.channel.Channel
import org.jboss.netty.channel._
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory
import org.jboss.netty.channel.group.DefaultChannelGroup
import java.util.concurrent.{Executors,CancellationException}
import org.jboss.netty.util.CharsetUtil
import scala.concurrent.{ Promise, Future }
import scala.concurrent.ExecutionContext.Implicits.global
/**
* @author mebinum
*
*/
class HttpRequestActor extends Actor {
//initialize response with default uninitialized …Run Code Online (Sandbox Code Playgroud) 我在页面上有以下内容 - 此Plunker中的完整代码
有一个自定义onEnter指令,当在聊天表单输入上按下enter时调用一个函数.下面的代码片段
//**HTML View**
<div ng-controller="mainCtrl">
<ul>
<li ng-repeat="chat in chatMessages">
{{chat.username}}<br/>
{{chat.message}}
</li>
</ul>
</div>
<form id="chatForm" name="chatForm" ng-controller="formCtrl">
<label for="chat-username">User: </label>
<input type="text" id="chat-username" class="chat__username" ng-model="chat.username" required>
<label for="chat-input">Chat: </label>
<input type="text" id="chat-input" class="chat__input" on-enter="sendChat()" ng-model="chat.message" required>
<a href="#" class="chat__submit icon-comments" id="chat-submit" ng-click="sendChat()" ng-disabled="isChatValid()">Chatme</a>
</form>
//**Javascript**
app.controller('formCtrl',function($scope,Chats){
$scope.sendChat = function() {
if($scope.isChatValid()) {
return;
}
console.log(JSON.stringify($scope.chat));
var msg = {};
angular.copy($scope.chat,msg);
Chats.data.push(msg);
};
$scope.isChatValid = function() {
return $scope.chatForm.$invalid;
};
});
Run Code Online (Sandbox Code Playgroud)
问题是input( …