如果我有像下面这样的websocket:
def websocket: WebSocket = WebSocket.accept[String, String] { _ =>
ActorFlow.actorRef(out => LightWebSocketActor.props(out))
}
Run Code Online (Sandbox Code Playgroud)
供参考,这是LightWebSocketActor
:
class LightWebSocketActor(out: ActorRef) extends Actor {
val topic: String = service.topic
override def receive: Receive = {
case message: String =>
play.Logger.debug(s"Message: $message")
PublishService.publish("true")
out ! message
}
}
object LightWebSocketActor {
var list: ListBuffer[ActorRef] = ListBuffer.empty[ActorRef]
def props(out: ActorRef): Props = {
list += out
Props(new LightSocketActor(out))
}
def sendMessage(message: String): Unit = {
list.foreach(_ ! message)
}
}
Run Code Online (Sandbox Code Playgroud)
这是使用akka
websocket方法. …
unit-testing specifications scala playframework playframework-2.5
在mix.exs
你可以声明依赖性,如:
def deps do
[{:plug, "~> 1.0"}]
end
Run Code Online (Sandbox Code Playgroud)
为什么它需要"〜>"而不是简单地在元组的第二部分上的版本.
我已经看到,如果它从git获得依赖,你可以编写依赖,如:
def deps do
[{:plug, git: "git://github.com/elixir-lang/plug.git"}]
end
Run Code Online (Sandbox Code Playgroud) 如果您期望收到类型A
或类型B
( Either[A, B]
) 的 Json,您如何为其编写解码器?
例如,假设您正在为外部 API 构建一个客户端,该客户端可以使用一些预期的 Json 结构进行应答:
{
"fieldA": "value",
"fieldB": "value2"
}
Run Code Online (Sandbox Code Playgroud)
或者如果出现问题,它将使用带有字段的对象进行响应error
:
{
"error": "Your request was wrong"
}
Run Code Online (Sandbox Code Playgroud)
然后你想要一个具有这些结构之一的实例:
{
"fieldA": "value",
"fieldB": "value2"
}
Run Code Online (Sandbox Code Playgroud)
如何为一种响应结构或另一种响应结构编写解码器?
我想通过控制台将动态数字添加到dynamodb中表的关键字段,例如0,1,2,...
我有contact_us
想要的列,其列具有自动ID增量。请帮我解决一下这个。
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
public class ContactUs extends HttpServlet {
String con_id;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
// ...
// ...
Map<String, AttributeValue> item = newRecord();
//System.out.println(item);
// ...
dbObject.addRecord(item, "contact_us");
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private Map<String, AttributeValue> newRecord() {
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
con_id = getConID();
dateTimeTimestamp = new SysDateTimeTimestamp();
//Need to add con_id which will be …
Run Code Online (Sandbox Code Playgroud) 我有一个case类,每个字段都是可选的,例如:
case class Foo(name: Option[String],
phone: Option[String],
email: Option[String])
Run Code Online (Sandbox Code Playgroud)
我试图为我的案例类创建手动解码器,发现解码器类似于:
implicit val decoder: Decoder[Foo] = (c: HCursor) => {
for {
name <- c.downField("name").as[String]
phone <- c.downField("phone").as[String]
email <- c.downField("email").as[String]
} yield {
new Foo(name, phone, email)
}
}
Run Code Online (Sandbox Code Playgroud)
但检查downField
方法是,如果未设置该字段,则光标将移至FailedCursor
,因此会出现错误。
我如何期望某个字段为可选字段,None
如果未定义则返回?