diff --git a/server/api/api.lisp b/server/api/api.lisp index 7ccff24..476f3f9 100644 --- a/server/api/api.lisp +++ b/server/api/api.lisp @@ -38,8 +38,6 @@ p-request))) - - (defmethod handle-new-game ((api api) data) (with-slots (gm) api (let* ((game-id (create-game gm))) @@ -80,7 +78,9 @@ :element-type 'character :adjustable t :fill-pointer 0))) - (format reason "~a~%" condition :escape nil) + (if (typep condition 'jonathan.error:) + (format reason "{\"type\":\"error\",\"message\":\"Failed to parse JSON\"}~%" condition :escape nil) + (format reason "{\"type\":\"error\",\"message\":\"~a\"}~%" condition :escape nil)) reason)))) diff --git a/server/packages.lisp b/server/packages.lisp index 2b6a5a2..d512691 100644 --- a/server/packages.lisp +++ b/server/packages.lisp @@ -16,6 +16,6 @@ (defpackage :remote-snake-server (:nicknames :rss) - (:use :common-lisp :usocket :remote-snake-server-api) + (:use :common-lisp :usocket :remote-snake-server-api :cl-strings) (:export #:start)) diff --git a/server/remote-snake-server.asd b/server/remote-snake-server.asd index b8bcfe5..598f270 100644 --- a/server/remote-snake-server.asd +++ b/server/remote-snake-server.asd @@ -3,7 +3,7 @@ :description "Remote Snake Server." :version "0.0.1" :author "Loic Guegan" - :depends-on ( "usocket" "jonathan") + :depends-on ( "usocket" "jonathan" "cl-strings" ) :components ((:file "packages") (:module "game" :depends-on ("packages") diff --git a/server/server.lisp b/server/server.lisp index bf778b1..f1417c5 100644 --- a/server/server.lisp +++ b/server/server.lisp @@ -9,11 +9,10 @@ (defun handle-client (client-socket) (unwind-protect ; To be sure to close the client socket - (let ((data (read-line (usocket:socket-stream client-socket))) - (request "")) - (loop while (not (equalp data "#EOF")) do - (setf request (concatenate 'string request data)) - (setf data (read-line (usocket:socket-stream client-socket) nil nil))) + (let* ((request (read-line (usocket:socket-stream client-socket)))) + (loop while (eq (search "#EOF" request) nil) do + (setf request (concatenate 'string request (read-line (usocket:socket-stream client-socket) nil nil)))) + (setf request (cl-strings:replace-all request "#EOF" "")) (format (usocket:socket-stream client-socket) (handle-request *server-api* request)) (force-output (usocket:socket-stream client-socket))) (usocket:socket-close client-socket)))